admin管理员组文章数量:1424970
I want to detect when the user has hit the back button. Essentially, when a user goes from the /projects to /projects/foo the nav style changes.
<Route path="/projects" ponent={(props, state, params) =>
<Projects {...props} />
</Route>
This works fine when detecting an onClick event from /projects to a specific project, but when the user hits the back button ponentDidMount
is not called... I'm trying to use this.props.location.pathname
to determine the specific path, but as I need to set state I cannot do this inside render
.
Is there a "react" way of going about this? Thx
I want to detect when the user has hit the back button. Essentially, when a user goes from the /projects to /projects/foo the nav style changes.
<Route path="/projects" ponent={(props, state, params) =>
<Projects {...props} />
</Route>
This works fine when detecting an onClick event from /projects to a specific project, but when the user hits the back button ponentDidMount
is not called... I'm trying to use this.props.location.pathname
to determine the specific path, but as I need to set state I cannot do this inside render
.
Is there a "react" way of going about this? Thx
Share Improve this question asked Aug 17, 2017 at 2:59 vesperaevesperae 1,3012 gold badges19 silver badges28 bronze badges1 Answer
Reset to default 4As you noted, ponentDidMount
will only be called once when the ponent is mounted. Instead, use the lifecycle hook ponentWillReceiveProps
. Here is the documentation.
You can use this to conditionally setState if you're at the desired/correct path. This will update the state which will then re-render the ponent.
UPDATE:
Since posting this answer ponentWillReceiveProps
has been deprecated. You'll need to use shouldComponentUpdate
or getDerivedStateFromProps
depending on your use case. More info on that here.
本文标签: javascriptReact Routerlifecycle when browser39s back button is clickedStack Overflow
版权声明:本文标题:javascript - React Router, lifecycle when browser's back button is clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745426522a2658138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论