How to update an object with setState in ReactJS?
We can update an object in React using the setState() method. Every component in React inherits the setState() method from its Base component name Component. setState() method tells React that we are updating the state, then it will figure out what part of the state is changed, and based on that, it will bring the DOM in sync with the virtual DOM.
We pass an object in the setState() method as an argument. The properties of that object will merge with what we have in the state object or override those properties if they already exist.
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
Project Structure: It will look like the following.
Step to Run Application: Run the application using the following command from the root directory of the project:
Output: Click on increment button to increase the value of count.