我正在尝试导入browserHistory,但是出现类似这样的错误。
./src/App.js
Attempted import error: 'browserHistory' is not exported from 'react-
router'.
我的代码是:
import React, { Component } from "react";
import { browserHistory } from "react-router";
import App from "./App";
class Home extends Component {
home = () => {
browserHistory.push("/home");
};
state = {};
render() {
return <button onClick={this.home} className="btn btn-outline-light">
Logout
</button>;
}
}
答案 0 :(得分:0)
据我了解,您的主要目的是以正确的方式更改浏览器URL,而不是实际导入browserHistory
对于react-router
v4:
您应该从HOC withRouter
中受益。以下一种方式应用它,这会将一些其他道具传递到您的组件中,从而可以浏览浏览器的历史记录
import { withRouter } from "react-router";
class Home extends Component {
home = () => {
// - you can now access this.props.history for navigation
this.props.history.push("/home");
};
render() {
return (
<button onClick={this.home} className="btn btn-outline-light">
Logout
</button>;
)
}
const HomeWitRouter = withRouter(Home);