When you declare a component as a function or a class, it must never modify its own props.

Posted by Editorial Staff | Updated on

When you declare a component as a function or a class, it must never modify its own props.

When you call setState() in the component, React merges the object you provide into the current state.

For example, let us take a facebook user with posts and comments details as state variables,

Now you can update them independently with separate setState() calls as below,

As mentioned in the above code snippets, this.setState({comments}) updates only comments variable without modifying or replacing posts variable.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers