Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - setState in componentWillReceiveProps

Is this legal?

componentWillReceiveProps: function(nextProps) {         if (typeof nextProps.contact != 'undefined') {             this.setState({forename: nextProps.contact.forename});             this.setState({surname: nextProps.contact.surname});             this.setState({phone: nextProps.contact.phone});             this.setState({email: nextProps.contact.email});         }     } 

Because I don't know how to fill my inputs and still be able that the user can edit the inputs. So I came up with this solution instead of trying to manipulate the this.props.

Any suggestions?

like image 844
Bene Avatar asked Aug 06 '15 15:08

Bene


People also ask

Can I call setState in componentWillMount?

componentWillMount() Safe to use setState ? Yes! In componentWillMount we can access the initial props and state that is defined in the constructor here.

How do I use componentWillReceiveProps in React?

ReactJS – componentWillReceiveProps() MethodThis method is used during the updating phase of the React lifecycle. This function is generally called if the props passed to the component change. It is used to update the state in response with the new received props.

Can I setState in componentDidUpdate?

You may call setState() immediately in componentDidUpdate() but note that it must be wrapped in a condition like in the example above, or you'll cause an infinite loop. It would also cause an extra re-rendering which, while not visible to the user, can affect the component performance.

Can we setState in componentWillReceiveProps?

The only reason componentWillReceiveProps exists is to give the component an opportunity to setState. So yes, any state you set synchronously in it will be processed together with the new props.


2 Answers

Your code is legal according to react documentation.

You also may consider to put this code inside getInitialState method as according to another react doc initializing from props is not an anti-pattern.

You also can replace several calls with one setState method call:

 this.setState({forename: nextProps.contact.forename,                 surname: nextProps.contact.surname,                 phone: nextProps.contact.phone,                 email: nextProps.contact.email}); 
like image 65
Mikhail Romanov Avatar answered Sep 28 '22 05:09

Mikhail Romanov


As of React v16.6.3 this is considered UNSAFE and componentWillReceiveProps is marked for deprecation. The removal is planned to happen in version 17.

Note:

Using this lifecycle method often leads to bugs and inconsistencies, and for that reason it is going to be deprecated in the future.

If you need to perform a side effect (for example, data fetching or an animation) in response to a change in props, use componentDidUpdate lifecycle instead.

For other use cases, follow the recommendations in this blog post about derived state.

If you used componentWillReceiveProps for re-computing some data only when a prop changes, use a memoization helper instead.

If you used componentWillReceiveProps to “reset” some state when a prop changes, consider either making a component fully controlled or fully uncontrolled with a key instead.

In very rare cases, you might want to use the getDerivedStateFromProps lifecycle as a last resort.

In your case you should use componentDidUpdate instead.

like image 26
gyosifov Avatar answered Sep 28 '22 06:09

gyosifov