react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

Document that syncing props and state is usually a bad idea

Open gaearon opened this issue 7 years ago • 10 comments
trafficstars

We used to have a "tip" about this on the old website (a few years ago), but not anymore.

I think maybe we need to revive that section because too many people attempt to derive state from props—either in constructor or cWRP—without realizing they could just do this work in render. In 90% of cases that's exactly what they need to do.

See also https://mobile.twitter.com/EntriaTech/status/934139022204653569.

gaearon avatar Nov 24 '17 19:11 gaearon

Hey @gaearon can I take this? Could you tell what to we need to add ?

jyash97 avatar Nov 24 '17 20:11 jyash97

@jyash97 the issue is yours!

I've added an "in-progress" label so that others will know not to start work on the issue. If you change your mind about the issue, no worries! Just let me know so that I can remove the label and free it up for someone else to claim.

As far as what to add, how about starting with a disclaimer in State and Lifecycle and FAQ: State?

alexkrolick avatar Nov 24 '17 23:11 alexkrolick

#341 Hey @alexkrolick I have done the changes, please have a look at it

jyash97 avatar Nov 25 '17 05:11 jyash97

Hey @jyash97. The outcome of #341 didn't actually end up related to this issue, but rather to #351, so I'm going to mark this issue as no longer in progress. If you'd like to send another PR that implements the suggestion from this issue though, let us know and we'll mark it back in progress. 😄

bvaughn avatar Nov 29 '17 21:11 bvaughn

@bvaughn Can you have a look at the first commit of #341, I added topics like what should come in State, is this the good way of tackling this issue?

jyash97 avatar Nov 30 '17 02:11 jyash97

At a glance the section "What Shouldn’t Go in State?" seems like what this issue is asking for.

bvaughn avatar Nov 30 '17 16:11 bvaughn

Okay let me try one more time to update this content

jyash97 avatar Nov 30 '17 17:11 jyash97

I think that's just another explanation of why state and props are different but this issue is asking for more of a warning about why syncing props to state is generally unnecessary.

bvaughn avatar Dec 04 '17 17:12 bvaughn

@bvaughn Sorry for being late on the issue, but have updated it now.

jyash97 avatar Jan 01 '18 18:01 jyash97

@gaearon is this still needed or is it covered in the hook version by: https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes

rickhanlonii avatar May 17 '25 18:05 rickhanlonii