며칠 전 들었던 노마드코더 강의 내용은 함수형 컴포넌트의 훅을 사용하지 않았었다(아마 강의 촬영이 좀 전이라 그런 듯 ㅠㅠ). 예를 들면 connect
노마드코더 리덕스 무료 강의
const FunctionalComponent = () => {
const exampleState = useSelector(state => state.example)
return <div>{exampleState}</div>
}
const FunctionalComponent = () => {
const dispatch = useDispatch();
const dispatchFunc = () => {
dispatch({type: "increment"})
}
}
store와 reducer 코드 작성을 간소화할 수 있다.
const counterSlice = createSlice({
name: "counter", // reducer 이름
initialState: {
counter: 0,
showCounter:true,
}, // 초기화
reducers: { // reducer들. 메서드 형태. 메서드 이름이 action.type
increment(state) {
state.counter++;
},
decrement(state) {
state.counter--;
},
increase(state, action) {
state.counter = state.counter + action.amount;
},
increment(state) {
state.showCounter = !state.showCounter;
},
}
})
const store = configureStore({
reducer:counterStore.reducer
})