-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
componentWillReceiveProps
: how to know when dynamic segment changes?
#584
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
Comments
To be clearer, right now I feel like I have to write something like this in my components: var MyComponent({
componentWillReceiveProps: function () {
if (!_.isEqual(this._prevParams, this.getParams())) {
this._prevParams = _.clone(this.getParams())
this.fetchSomeData()
}
}
// ...
}); |
I ran into this too when upgrading the router. We had to solve it in the same way. Perhaps a better way would be to introduce a |
I usually do this: parseUserId(props) {
return parseInt(props.params.userId, 10);
},
componentWillReceiveProps(nextProps) {
var userId = this.parseUserId(this.props),
nextUserId = this.parseUserId(nextProps);
if (userId !== nextUserId) {
this.userWillChange(nextUserId);
}
},
userWillChange(userId) { ... } Why do you need to save previous params? |
@gaearon I don't thank that works with latest react-router? We mean comparing the last props.params.userId with nextProps.params.userId |
Why wouldn't it? But you're right, I meant @abergs I fixed the example. |
@abergs But you are still free to pass them to route handlers if you like. (I do.) e.g. router.run((Handler, state) => {
RouterActionCreators.changeRoute(state);
React.render(<Handler {...state} />, document.body); // this will pass query, params, like before
}); and later return <RouteHandler {...this.props} /> in nested handlers. This'll give you familiar |
Another way do to the same is to follow last comment in example you linked to: // Also, if you're using a flux-style app, you can trigger a "transition"
// action in the `run` callback with the params/query in the payload, then
// subscribe in your handlers to the store that grabs the data. |
see #579 (comment) |
tl;dr: what is the recommended way in
componentWillReceiveProps
to find the previous dynamic segment?Longer:
I understand the note about dynamic segments: the lifecycle hook to fetch data when a dynamic segment changes is
componentWillReceiveProps
. Cool.In React,
componentWillReceiveProps
receives as an argument thenextProps
. These can be compared to this.props and used to determine whether a relevant property changed and data should be fetched.What I want to do is only fetch new data in
componentWillReceiveProps
if the dynamic segment changes. Significantly, in the presence of other props, I cannot assume thatcomponentWillReceiveProps
is called only when the dynamic segment changes. However I haven't found a way to recover the previous routing params.I can easily work around by setting an instance variable in
componentWillReceiveProps
to record the current routing params for comparison on the next invocation ofcomponentWillReceiveProps
. That seems sort of nasty and stateful.I hope I haven't misunderstood. But if I have understood correctly, then the routing params context is mutable and the previous route is lost, and cannot be compared against. That seems contrary to the concept of
componentWillRecieveProps
.The text was updated successfully, but these errors were encountered: