React useReducer 사용법 (1) 썸네일형 리스트형 useReducer 복잡한 상태관리 로직분리 useState로 데이터를 관리하다보면 crud할때 계속 setState를 사용하다보니 코드줄이 길어짐 => 밖으로 빼서 깔끔하게하기위함 로직은 useState와 매우 비슷함 1. 기존 useState를 지우고 그자리에 useReducer를 넣는다. const [data, dispatch] = React.useReducer(reducer, []) 2. 외부에(app컴포넌트 밖 혹은 다른파일) reducer 함수에 action type에 따른 로직 준비 const reducer = (state, action) => { switch(action.type){ case 'INIT':{ return action.data } case 'CREATE':{ const newItem = { ...action.data }.. 이전 1 다음