react icon indicating copy to clipboard operation
react copied to clipboard

componentDidReceiveProps Please

Open iammerrick opened this issue 11 years ago • 94 comments

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.

iammerrick avatar Feb 27 '15 06:02 iammerrick

Why not setup(props)?

syranide avatar Feb 27 '15 12:02 syranide

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)
}

insin avatar Feb 27 '15 13:02 insin

@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.

iammerrick avatar Feb 27 '15 17:02 iammerrick

+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.

JakeLingwall avatar Feb 27 '15 17:02 JakeLingwall

+1, I also find this frustrating.

No longer find this to be an issue.

gaearon avatar Mar 01 '15 12:03 gaearon

+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.

dandelany avatar Mar 20 '15 01:03 dandelany

+1

monsanto avatar Mar 25 '15 15:03 monsanto

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.

gaearon avatar Mar 25 '15 15:03 gaearon

+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.

mienaikoe avatar Apr 16 '15 19:04 mienaikoe

+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.

tristang avatar Apr 29 '15 00:04 tristang

+1

dev-rkoshlyak avatar May 13 '15 20:05 dev-rkoshlyak

+1

jnmandal avatar Sep 14 '15 19:09 jnmandal

What about componentWillMount and componentWillUpdate @iammerrick

quantizor avatar Sep 15 '15 14:09 quantizor

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?

gaearon avatar Sep 15 '15 23:09 gaearon

+1

devjoca avatar Sep 18 '15 21:09 devjoca

+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);
}

kmalakoff avatar Sep 30 '15 04:09 kmalakoff

Thoughts?

componentWillReceiveProps() {
  setTimeout(()=> {
    if(this.isMounted()) {
      this.componentDidReceiveProps();
    }
  });
}
componentDidReceiveProps() {
  UserActions.load(this.props.id);
}

What do you think?

YourDeveloperFriend avatar Sep 30 '15 17:09 YourDeveloperFriend

@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.

kmalakoff avatar Oct 01 '15 12:10 kmalakoff

+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.

robyoder avatar Nov 28 '15 05:11 robyoder

+1

micky2be avatar Dec 04 '15 07:12 micky2be