• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    해결됨

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

23.06.02 16:02 작성 조회수 272

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를 붙일 때와 안붙일때 어떤 차이가 있나요?

답변 2

·

답변을 작성해보세요.

0

i1004gy님의 프로필

i1004gy

질문자

2023.06.02

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

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

0

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

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