강의

멘토링

로드맵

Inflearn brand logo image

Inflearn Community Q&A

i1004gy4926's profile image
i1004gy4926

asked

Learning React while making web games

8-7. Victory condition check and timer

state.halted와 그냥 halted의 차이점이 궁금합니다

Resolved

Written on

·

406

0

    if (
        state.data.row * state.data.cell - state.data.mine ===
        state.opendCount + openedCount
      ) {
        halted = true;
        result = "승리하셨습니다";
      }
      return {
        ...state,
        tableData,
        opendCount: state.opendCount + openedCount,
        halted,
        result,
      };

위 코드는 제로초님이 작성하신 코드입니다

여기서 halted와 result에 값을 갱신할때 state.halted 이렇게 한 것이 아니고 그냥 halted=true이렇게 갱신을 하셨습니다

    if (
        state.data.row * state.data.cell - state.data.mine ===
        state.opendCount + openedCount
      ) {
        state.halted = true;
        state.result = "승리하셨습니다";
      }
      return {
        ...state,
        tableData,
        opendCount: state.opendCount + openedCount,
        halted: state.halted,
        result: state.result,
      };

왜 state를 안 붙이고 그냥 할 수 있을까 해서 state를 붙여서 해봤더니 return 구문에서 halted: state.halted 이거 처럼 따로 갱신하는 구문이 필요했습니다 state를 붙일 때와 안붙일때 어떤 차이가 있나요?

react

Answer 2

0

i1004gy4926님의 프로필 이미지
i1004gy4926
Questioner

아 state는 위에 inital state에 정의된 값 불러오는 거고

halted 하고 result은 위에 let으로 정의가 되었네요 잠시 착각했습니다 감사합니다

0

zerocho님의 프로필 이미지
zerocho
Instructor

return { ...state, tableData, opendCount: state.opendCount + openedCount, };

이렇기만 해도 되긴 할텐데요. 근데 리액트에서는 state를 수정하는 게 아닙니다.

i1004gy4926's profile image
i1004gy4926

asked

Ask a question