React 컴포넌트의 state를 변경해야 할 땐, 무조건 불변성을 유지해야한다.
업데이트 하는 과정에서 기존의 객체의 값을 직접 수정하면 안된다.const state = { users: [ { id: 1, username: "Kim" } ] } // No state.users.push({ id:2, username: "Lee" }) //No state.users[0].username = "Lee"
const users = {
user: [
{
id: 1,
username: "Kim",
email: "111@naver.com",
},
{
id: 2,
username: "Lee",
email: "222@naver.com",
},
],
};
const nextUsers = [...users];
nextUsers[1] = {
...users[1],
email: "Cheo",
};
import produce from "immer";
const users = {
user: [
{
id: 1,
username: "Kim",
email: "111@naver.com",
},
{
id: 2,
username: "Lee",
email: "222@naver.com",
},
],
};
const nextUsers = produce(users, draft => {
draft[1].username = "Cheo"
})