+1 vote
in Redux by
Can we combine multiple reducers? if yes, then how?

1 Answer

0 votes
by
Yes, we can combine multiple reducers using - combineReducers.

import React, { Component } from "react";

import { createStore, combineReducers } from "redux";

const initialLangState = {

  lang_code: "en"

};

const initialUserState = {

  user_details: {}

};

const language = (state = initialLangState, action) => {

  let newState = { ...state };

  if (action.type == "SET_LANGUAGE") {

    newState.lang_code = action.lang_code;

  }

  return newState;

};

const user = (state = initialUserState, action) => {

  let newState = { ...state };

  if (action.type == "SET_USER") {

    newState.user_details = action.user_details;

  }

  return newState;

};

let rootReducers = combineReducers({

  language: language,

  user: user,

});

const store = createStore(rootReducers);

Related questions

0 votes
asked Feb 22, 2020 in NoSQL by SakshiSharma
0 votes
asked Mar 22, 2022 in TypeScript - JavaScript's Superset by sharadyadav1986
...