react-router4 如何监听url变化,要通过url的变化来做一些 <HashRouter history={hashHistory} onEnter={this.routeUpdate.bind(this)}>
用componentWillReceiveProps也不行,
react-router4 如何监听url变化,要通过url的变化来做一些 <HashRouter history={hashHistory} onEnter={this.routeUpdate.bind(this)}>
用componentWillReceiveProps也不行,
componentDidMount() {
this.context.router.history.listen((route) => {
if(route.pathname === '/xxx') {
console.log(1);
}
});
}
componentWillUpdate(nextProps) 或者 componentDidUpdate(prevProps)
componentWillUpdate(nextProps) {
if (this.props.location !== nextProps.location) {
// 路由变化
}
}
3 回答1.9k 阅读✓ 已解决
1 回答1.6k 阅读✓ 已解决
4 回答1.6k 阅读✓ 已解决
2 回答2.5k 阅读✓ 已解决
1 回答2.6k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
2 回答1.5k 阅读✓ 已解决
分2种情况:
1、普通react组件(指不是通过Route导入的组件)
普通组件需要自己定义上下文
2、Route导入的组件
Route组件已经封装好了上下文,并且设置了props可用。
原生js的办法是: