ReasonJun

Redux: CombineReducers 본문

Frontend/Redux

Redux: CombineReducers

ReasonJun 2023. 8. 19. 14:05
728x90

CombineReducers is a Redux helper function that takes an object of reducers as input and returns a single reducer that combines the functionality of all the reducers in the object. This is useful when your application has a complex state that is managed by multiple reducers.

 

The syntax for using CombineReducers is as follows:

const rootReducer = combineReducers({
  counter: counterReducer,
  todos: todosReducer,
});

In this example, the counterReducer and todosReducer are two separate reducers that manage the counter and todos state properties, respectively. The combineReducers function combines these two reducers into a single reducer called rootReducer.

 

The rootReducer is then passed to the createStore function to create the Redux store for the application.

Here are some of the benefits of using CombineReducers:

  • It makes it easier to manage complex state.
  • It helps to improve code readability and maintainability.
  • It reduces the number of reducers that need to be defined.

Here are some of the things to keep in mind when using CombineReducers:

  • The keys of the object passed to CombineReducers must be the names of the state properties that the reducers manage.
  • The reducers passed to CombineReducers must be pure functions.
  • CombineReducers does not check for conflicts between the reducers. It is up to the developer to ensure that the reducers do not conflict with each other.
728x90

'Frontend > Redux' 카테고리의 다른 글

Redux: toolkit  (0) 2023.08.19
Redux : middleware / Thunk  (0) 2023.08.19
Redux : Provider / useSelector / useDispatch  (0) 2023.08.19
Redux: strict unidirectional data flow  (0) 2023.08.19
Redux : What is Redux  (0) 2023.08.19
Comments