본문 바로가기
TIL/REACT

dispatch ( { } ), action 객체를 실행. reducer 기록

by koreashowme 2019. 10. 17.

dispatch (   {    }    ),   {action} 객체를 실행한다.

 

ex)

 

const SET_WINNER = 'SET_WINNER';

 

const reducer = ( state, action ) => {

 switch ( action.type ) {

   

   case SET_WINNER:  //state.winner = action.winner; 이렇게 하면 안됨.

 

return {

      ...state, // 기존 state spread 후, action 실행. 

      winner : action.winner,

 

  }

}

}

const onClickTable = useCallBack( ( ) => {

 

   // action객체를 dispatch해서 state를 바꿔준다.

  dispatch( { type : 'SET WINNER' , winner: ' 0 ' } );

 

}

 

 

 

 

 

State 직접 건들 수 없음. 아무도 직접 수정할 수 없음.

 

state 수정 하려면 =>

action을 만들 고 dispatch를 해야지 state를 수정할 수 있음.

 

action 어떻게 처리할지는 =>

reducer에서 관리를 한다.

 

이벤트 발생 할 때 action을 dispatch 해서 state를 바꿔야함. 

어떻게 바꾸는지는 reducer기록함.

 

 

 

 

 

 

comment