componentDidReceiveProps Please
I would like to humbly request a componentDidReceiveProps hook, often times I would like to do something on both componentWillMount and componentWillReceiveProps, but because this.props hasn't been set yet I am forced to pass props around instead of reading directly from this.props.
Before New Hook
componentWillMount() {
this.setup(this.props.id);
}
componentWillReceiveProps(next) {
this.setup(next.id);
}
setup(id) {
UserActions.load(id);
}
After New Hook
componentWillMount() {
this.setup();
}
componentDidReceiveProps() {
this.setup();
}
setup() {
UserActions.load(this.props.id);
}
In this simple example it may seem like a small thing, but often times the passing of props runs deep and instead of conveniently referencing this.props one is forced to plumb the props throughout the component.
Please consider adding componentDidReceiveProps as a hook to leverage the same code that is leveraged in componentWillMount without forcing both to plumb props throughout the component.
Why not setup(props)?
Looking through my own projects I can see I've done this where I had similar needs (another one is deriving a chunk of state based on props), just passing in a different set of props when needed so you only have to pass something extra when reusing, without duplicating knowledge of which props are needed:
setup(props) {
props = props || this.props
UserActions.load(props.id)
}
@syranide The trouble is when setup needs to call methods that also need props, which needs to call methods which also needs props... Eventually your entire component is plumbing around props.
+1 seems like a bunch of needless wiring up in the app with the current pattern. Could be a concise standardized way to solve the problem. Seen a bunch of people get burned on this.props inside of ComponentWillReceiveProps, a clear sign that it's not intuitive.
+1, I also find this frustrating.
No longer find this to be an issue.
+1, I am also finding myself passing around props a lot. Similar to @insin above I was using default params for awhile:
setup(props = this.props) {
doSomething(props);
}
But decided it was an anti-pattern due to the subtle bugs it can cause if you forget to pass newProps.
+1
I think the reason it's not available is because this.props and this.state always correspond to rendered values. There's no way to implement componentDidReceiveProps that doesn't trigger another render without breaking this constraint.
Most of the times, if you're using componentWillReceiveProps, you actually want either a higher order component a la Relay, or something like observe hook proposed for React 0.14.
+1
Also, you can implement componentDidReceiveProps without changing this.props or this.state. If all you need to do is read from these, then you won't trigger another render. If you're writing to props or state within this proposed function call, then you're shooting yourself in the foot, but that's the same case for every other method in the lifecycle.
+1
I want to be able to respond to the new props event when shouldComponentUpdate returns false, so in my case I can't use componentDidUpdate.
+1
+1
What about componentWillMount and componentWillUpdate @iammerrick
I want to be able to respond to the new props event when shouldComponentUpdate returns false, so in my case I can't use componentDidUpdate.
Use componentWillReceiveProps?
+1
+1 this helps with DRY code and simplifying logic.
I would be open for componentDidReceiveProps to be called on the initial setup to make the example starting this thread just a single function:
componentDidReceiveProps() {
UserActions.load(this.props.id);
}
Thoughts?
componentWillReceiveProps() {
setTimeout(()=> {
if(this.isMounted()) {
this.componentDidReceiveProps();
}
});
}
componentDidReceiveProps() {
UserActions.load(this.props.id);
}
What do you think?
@YourDeveloperFriend I think it depends on how it works relative to other lifecycle hooks and rendering delays due to cascading timeouts on nested components.
These sorts of lifecycle hooks should be called synchronously in a pass that is guaranteed to be called before render. I haven't studied React's codebase, but I'm assuming render isn't called on a timeout.
Most likely, the best solution is going to be something along the lines of registering pre-rendering hooks or to mark components that have had their props changed so the render logic calls componentDidReceiveProps before calling render.
+1 please. Having the same issue where I have to pass props around to just about every method in my component class. So ugly.
Nah, I'm good. There are better solutions.
+1