I've built a global state provider context, and a reducer with the useReducer Hook. I'm realizing that combining multiple reducers like I would in redux is problematic. Is there a good way to do this? I have seen people importing combineReducers from redux itself, and that seems like it kind of defeats the point. Does anyone have any insight on this?
Having multiple reducers become an issue later when we create the store for our redux. To manage the multiple reducers we have function called combineReducers in the redux. This basically helps to combine multiple reducers into a single unit and use them.
Using Redux comes at a cost. Installing these dependencies increases our final bundle size. On the contrary, Context APIs are a part of React, so our bundle size remains the same. Boilerplate code: With Redux, we need to have an exhaustive setup, we need to build a store, and we need to dispatch actions.
One of the biggest problems was “prop drilling”, which was common with nested components. The solution was to use a state management library like Redux. This, unfortunately, came with the expense of writing boilerplate code. But now it's possible to replace Redux with React Hooks and the Context API.
Not sure this is what you're looking for, but I have used something like below to combine multiple reducers. It actually reduces the reducers. Not actually like redux combineReducers
with key/value.
const reduceReducers = (...reducers) => (prevState, value, ...args) =>
reducers.reduce(
(newState, reducer) => reducer(newState, value, ...args),
prevState
);
I would be used like:
function reducerA(state, action) {
switch (action.type) {
case "increment":
return { ...state, count: state.count + 1 };
case "decrement":
return { ...state, count: state.count - 1 };
default:
return state;
}
}
function reducerB(state, action) {
switch (action.type) {
case "double":
return { ...state, count: state.count * 2 };
case "halve":
return { ...state, count: state.count / 2 };
default:
return state;
}
}
export default reduceReducers(reducerA, reducerB);
Then the Component:
import reducers from "./reducers";
function Counter({ initialState = { count: 1 } }) {
const [state, dispatch] = useReducer(reducers, initialState);
return (
<>
Count: {state.count}
<button onClick={() => dispatch({ type: "increment" })}>+</button>
<button onClick={() => dispatch({ type: "decrement" })}>-</button>
<button onClick={() => dispatch({ type: "double" })}>x2</button>
<button onClick={() => dispatch({ type: "halve" })}>/2</button>
</>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With