강의

멘토링

커뮤니티

Inflearn Community Q&A

moonjong938083's profile image
moonjong938083

asked

[Renewal] Creating NodeBird SNS with React

Write a post, comment saga

불변성 유지하면서 변수 만들 때 질문이 있습니다.

Written on

·

304

0

불변성 유지하기 위해서 변수 만들떄

const postIndex = state.mainPosts.findIndex((v) => v.id === action.data.postId);
      const post = {...state.mainPosts[postIndex]}
      post.Comments = [dummyComments(action.data.content), ...post.Comments]
      const mainPosts = [...state.mainPosts]
      mainPosts[postIndex] = post

이러한 과정을 거치던데

const post = {...state.mainPosts[postIndex]}

부분이 왜 {}로 감싸져 있는걸까요??

대괄호인 []로 감싸져야 하는게 아닐까요??

그 아래인 const mainPosts = [...state.mainPosts]로 []로 감싸져 있는데 왜그런걸까요 

reactreduxexpressnodejsNext.js

Answer 1

0

zerocho님의 프로필 이미지
zerocho
Instructor

post는 일반 객체입니다. mainPosts는 배열이고요.

일반 객체는 {...}고, 일반 배열은 [...]로 얕은 복사합니다.

moonjong938083님의 프로필 이미지
moonjong938083
Questioner

아~ 그렇군요 이해했습니다^^

moonjong938083's profile image
moonjong938083

asked

Ask a question