Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: Difference between this.state= and this.setState [duplicate]

In React I have state variable name1. Is there any difference between. Is there any difference between

this.state.name1 = value;

And

this.setState({name : value});
like image 442
Denis Avatar asked Feb 24 '18 17:02

Denis


1 Answers

You will usually set the initial state like this:

this.state = {name1: value}

Then when you need to update it you will use setState:

this.setState(prevState => ({name1: newValue}));
like image 72
Sagiv b.g Avatar answered Oct 29 '22 14:10

Sagiv b.g