Updating objects in React State

Updating objects in React State

Just like Props, we should treat state objects as immutable (read-only).

For example, in the following code, we cannot update the property age of the user object in this way: user.age = 24

const [user, setUser] = useState(

{

name: 'Ehsan',

age: '32'

}

)

const handleClick = () => {

user.age = 24;

setUser(user);

}        

this way of updating is wrong, and it will not update our state, so the component will not re-render.

* The correct way of updating the state object is to create a new instance of an object with updated property, and then call the setState with this new object:

const handleClick = () => {

const newUser = {

name: 'Ehsan',

age: 24

}

setUser(newUser);

}        

Also, we can define the new object as below (spread operator):

const newUser = {

...newUser,

age: 24

}        

this syntax will copy all the previous properties and append the updated property.

ways to update the react state

The more professional way of updating an object in the state is as below (in one line!):

setUser({...user, age: 24});        

so, we don't need to define a new object (newUser).

Please note that the spread operator in javascript does shallow copy of objects.

It means that if we want to update a nested object in the state, the spread operator will only copy the first level of the object.

For example, imagine that our user object is as below:

{

  fullName: 'Ehsan Safari,

  age: 32,

  address: {

    city: 'Milan',

    zipCode: 10123

  }

}        
Initialize user state and update user object

now we are going to update the zip code of the user's address. If we just use the spread operator as before, we cannot achieve this goal. So we have to use spread twice as below:

setUser({...user, address: {...user.address, zipCode: 10321}});

#reactjs #state_object #update_object #javascript

Solaudeen Warith

Fullstack Developer | PHP lover | Learning Golang

1 年

Thanks sir, can you give some recommendations to learning react

Reza Gholami

Software Developer | Javascript, React, Next.js, Node.js

1 年

??? ??? ??? ...newUser ?? ???? ??? ...user

要查看或添加评论,请登录

Ehsan Safari的更多文章

  • Tips For Better Next.js Development...!

    Tips For Better Next.js Development...!

    Hi, dear connections, In this article, I listed 5 tips to improve your Next.js Development.

    2 条评论
  • Call Stack Concept in JavaScript

    Call Stack Concept in JavaScript

    JavaScript is a single-threaded programming language which means that it has only one call stack that is used to…

  • Prevent XSS Attacks in React.js

    Prevent XSS Attacks in React.js

    Imagine that we have a blog variable that contains some HTML tags. If we display the blog directly inside the JSX…

    6 条评论
  • Schema-Based Form Validation in React.js

    Schema-Based Form Validation in React.js

    There are several libraries in the market, that can provide React.js projects with the schema-based form validation.

    1 条评论
  • How to integrate TinyMCE into a React.js App

    How to integrate TinyMCE into a React.js App

    When it comes to choosing the best text editor for our application, TinyMCE is one of the top rich text editors in the…

  • Image Loading Optimization in Nextjs

    Image Loading Optimization in Nextjs

    ?? Image Loading Optimization in Next.js: For your NEXT.

  • ????? ???? ???? ?? pagination ???? ?? ??? ?????

    ????? ???? ???? ?? pagination ???? ?? ??? ?????

    ???? pagination ?? ???? ??? useQuery ?? ?? ??? ??? ????? ???: useQuery(["posts", currentPage],() =>…

社区洞察

其他会员也浏览了