combineReducers
combineReducers는?
리덕스를 사용하다보면 수많은 액션과 리듀서들이 생기게 돼서 복잡해진다.
리덕스는 이런 리듀서의 복잡함을 덜 수 있게 여러 리듀서를 하나로 합쳐 주는 combineReducers 함수를 제공한다.
이렇게 모든 리듀서들이 하나로 합쳐진 것을 rootReducer라고 부른다.
combineReducers 사용법
import { combineReducers } from 'redux';
import postReducer from './reducers/postReducer';
import commentReducer from './reducers/commentReducer';
const rootReducer = combineReducers({
post: postReducer,
comment: commentReducer,
});
export default rootReducer;
combineReducers 함수의 인자로 객체 형태로된 리듀서들을 넣는다.
이때 post, comment와 같이 객체의 키로 사용된 이름은 리덕스 루트 state 객체의 키가 된다.
'기록 > TIL' 카테고리의 다른 글
[TIL] Redux의 createSlice (0) | 2024.11.25 |
---|---|
[TIL] Redux의 미들웨어 (0) | 2024.11.24 |
[TIL] Redux의 useSelector, useDispatch (0) | 2024.11.22 |
[TIL] Context API와 Redux의 차이 / Redux Toolkit (0) | 2024.11.21 |
[TIL] 상태 관리 / Redux (1) | 2024.11.20 |