Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于react-router 父组件包裹withRouter可以触发子组件的 componentWillReceiveProps #157

Open
ckinmind opened this issue May 14, 2019 · 0 comments

Comments

@ckinmind
Copy link
Owner

ckinmind commented May 14, 2019

问题描述

  1. 有两个路由,指向同一个组件,类似/detail/detail/:id
  2. 这两个路由的切换是单页形式的
  3. 指向的这个组件包含有一个父子组件,我想在路由切换的时候触发子组件的componentWillReceiveProps,没有成功
  4. 但是如果我此时将父组件包裹一个withRouter,子组件就能触发componentWillReceiveProps
  5. 子组件已经包裹withRouter

评论:

  1. 本来我的思路是想在子组件触发componentWillReceiveProps时来处理因为路由切换而需要做的状态清理,但是路由切换的时候居然没有触发,觉得很神奇
  2. 然后,我在其父组件加了componentWillReceiveProps,路由切换时父组件触发了该方法,所以在父组件去做因为路由切换而带来的状态清理,然后我又心血来潮去掉了componentWillReceiveProps,只讲父组件包裹了withRouter,结果子组件又能在路由切换时触发componentWillReceiveProps了
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant