상태를 업데이트 할 때는 주로 useState를 사용해서 새로운 상태를 설정해준다.
useState 말고 useReducer를 사용해서도 상태를 관리할 수 있다.
useReducer를 사용하면 컴포넌트의 상태 업데이트 로직을 컴포넌트에서 분리시킬 수 있다.
function reducer(state, action) {
// 새로운 상태를 만드는 로직
// const nextState = ...
return nextState;
}
//example for action
//형태에 제약은 없다.
{
type: 'INCREMENT'
}
{
type: 'CHANGE_INPUT',
key: 'email',
value: 'tester@react.com'
}
const [state, dispatch] = useReducer(reducer, initialState);
dispatch({ type: 'INCREMENT' })
식으로 사용한다..참고
https://react.vlpt.us/basic/20-useReducer.html
https://goddaehee.tistory.com/311