Combine Reducers into One
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write combineReducers(reducers), where reducers is an object whose values are reducers for slices of state. Return a single reducer (state, action) for an object with the same keys, where state defaults to an empty object. Each key's reducer is called, in the order of the keys, with that key's slice of state and the action, and returns the new slice, so a slice reducer with a default parameter sets up its own start when there's no state yet. If every slice comes back as the very same value (===) and state has no keys besides the reducers' ones, return the state object you were given; otherwise return a new object with just the reducers' keys, keeping unchanged slices as they were. Never modify the state you're given. If a slice reducer returns undefined, throw an Error whose message includes that key.
Your code must pass
- Gives each reducer its own slice and the action
- Lets each slice set up its own start
- Returns the same state when no slice changes
- Returns a new object when a slice changes, keeping the others
- Never modifies the state it's given
- Works nested inside another combined reducer
- Throws when a slice reducer returns undefined