How to update nested state properties in ReactJS?
There are the following approaches to update nested state properties in ReactJS:
- Approach 1: We can create a dummy object to perform operations on it (update properties that we want) then replace the component’s state with the updated object.
- Approach 2: We can pass the old nested object using the spread operator and then override the particular properties of the nested object.
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.
Approach 1: App.js
Approach 2: App.js
Step to Run Application: Run the application using the following command from the root directory of the project: