Redux is very weird - until you get used to it. But don’t be disheartened because this is confusing you - that is normal.
Why is state being placed store and not in the messageReducer?
State is memory. The state is part of the store. That is where you are “storing” the state for the app. Individual React components can still have their own individual state, but keeping an app state in the redux store means that you don’t have to pass it around from component to component.
Why no state in the reducer? Because reducers don’t store data. They are a function they take in and old state and and action and return the new state.
Yes, this is weird. It sure tripped me up for a long time. But just keep doing it and it will start to make sense. In reality, in a “real” app you don’t usually set up your store like that, but in a way this is good because it lets you peak under the covers a bit.
I had this exact same question. The solution to this that FCC provides, like OP is feeling, seems counterintuitive to everything they’ve taught us so far.
Here’s what I came up with (that didn’t pass), which incidentally is EXACTLY how it’s presented on the next challenge!!!
const ADD = 'ADD';
function addMessage(message) {
return {
type: ADD,
message
};
};
const messageReducer = (state = '', action) => {
switch(action.type) {
case 'ADD':
return state.concat(action.message);
default:
return state
}
}
const store = Redux.createStore(messageReducer);