Skip to content
Related Articles

Related Articles

Improve Article

How to set an object key inside a state object in React Hooks?

  • Last Updated : 14 Jan, 2021

We can update a React hooks state object that has a nested object containing objects with index keys with the following approach, Before doing so, consider the following example:

Example: Following is the default state object:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

const [data, setData] = useState({
   name:'',
   contact:'',
   address:{
     0:{},
     1:{},
   }
})

Following is the desired output after updating a nested object:

{
  name:'',
  contact:'',
  address:{
    0:{"city":'jaipur'},
    1:{},
  }  
}

Solution: We can use the below syntax to set an object key inside a state object:



data.address[0].city = 'jaipur';
setData({...data});

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:

cd foldername

Filename: App.js

Javascript




import React, { Component, useState } from "react";
  
const App = () => {
  const [data, setData] = useState({
    name: '',
    contact: '',
    address: {
      0: {},
      1: {},
    }
  })
  
  const handleUpdate = () => {
    data.address[0].city = 'jaipur';
    setData({ ...data });
  }
  
  return (
    <div >
      <button
        style={{ margin: 100 }}
        onClick={() => { handleUpdate() }} >
        change state
      </button>
      { data.address[0].city + " "}
    </div>
  );
}
  
export default App;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: 

  • Following will be the output before clicking the button:

    Before Click

  • Following will be the output after clicking the button:

    After Click

My Personal Notes arrow_drop_up
Recommended Articles
Page :