import React from 'react'
import {
BrowserRouter as Router,
Route,
Link,
Switch,
BrowserRouter
} from 'react-router-dom'
import {unmountComponentAtNode, findDOMNode} from 'react-dom';
import createBrowserHistory from 'history/createBrowserHistory';
const history = createBrowserHistory();
const BasicExample = () => (
<BrowserRouter history={history}>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/not_find">404</Link></li>
</ul>
<hr/>
<div id="oops">
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/not_find" component={NotFound}/>
</Switch>
</div>
</div>
</BrowserRouter>
)
const Home = () => (
<div>
<h2>Home</h2>
</div>
)
const About = () => (
<div>
<h2>About</h2>
</div>
)
class NotFound extends React.Component {
constructor() {
super();
}
handleSubmit(e) {
const div = document.createElement('div')
const location = {
pathname: '/about',
state: { fromDashboard: true }
};
history.push(location)
}
componentDidMount() {
//alert(10)
}
componentWillUnmount() {
}
render() {
return (<div ref="wassup" onClick = {this.handleSubmit.bind(this)} className = "app-notfind-page">hi,404<div ref = 'oops' className="s1">111</div></div>)
}
}
export default BasicExample
React router v4 history.push() 组件不更新啊,代码如下,ui不更新啊
macsen110
#1
import {
BrowserRouter as Router,
Route,
Link,
Switch,
BrowserRouter
} from ‘react-router-dom’
将上面的 BrowserRouter as Router改成Router,因为BrowserRouter内置了history,而Router是支持传入history