Clarification for React and Redux: Extract State Logic to Redux

According to the challenge text:

Then create a reducer called messageReducer() that handles the state for the messages. The initial state should equal an empty array. This reducer should add a message to the array of messages held in state, or return the current state.

Link to challenge:

I find myself unsure if the instructions meant to say that state should be an empty array initially or did they mean a blank object? (isn’t state usually an object?)

Edit: I guess I’m confusing component states with the overall redux store state… hmmm.

No, it’s an empty array. In the end, the difference is trivial, but the series of challenges builds up to a message list (if I remember correctly), which can be stored in an array. In a more complex app, an object would be better suited, but in this case it should be enough.

1 Like

Right, it is the state of the redux store. Presumably you will have several reducers, and this one will be property on that object. But within the reducer, you will act on it as whatever that property of the overall store is. Each of the reducer states is a property on the Redux store object. Presumably there is one called store.message that is an array (as defined in the problem). Because of how combineReducers works, each reducer only sees its property of the overall store, so it thinks of it as state, even though it just part of the overall store.

Yes, it’s weird. But it works beautifully.

1 Like