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기록함.
'TIL > REACT' 카테고리의 다른 글
contextAPI, provider 설정, createContext( ) (0) | 2019.10.17 |
---|---|
클래스와 hooks 라이프사이클 비교. <reminder> memo (0) | 2019.10.12 |
useEffect 간단한 내용. (0) | 2019.10.12 |
메서드 안에 있는 함수 호출, 함수 최적화 하기. (0) | 2019.10.12 |
componentDidMount() , componentDidUpdate(), componentWillUpdate() (0) | 2019.10.11 |
comment