Confused on prevState vs intitialState vs currentState

Tell us what’s happening:

What are the differences? It says in these instructions initial state to be empty array but then the solution has previousState as empty array? Are they all interchangeable because they are all past versions of state?

Your code so far


// define ADD, addMessage(), messageReducer(), and store here:
const ADD = "ADD";

const addMessage = message => {
return {
  type: ADD,
  message
};
};


const messageReducer = (prevState = [], action) => {
switch(action.type) {
  case ADD:
  return [...prevState, action.message];
  default:
  return prevState;
}
};


const store = Redux.createStore(messageReducer);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Extract State Logic to Redux

Link to the challenge:

Hello there,

There is no difference. The name prevState is just that…a random name. The lesson/challenge just hopes to explain the fact that instead of writing this:

const initialState = []; // Most applications will use this
                        // method, because most applications have
                       // an initial state much more verbose.

const messageReducer = (prevState = initialState, action) => {...}

You can just write:

const messageReducer = (prevState = [], action) => {...}

However, it is important to remember that, if there is a value passed to the reducer (not []), then prevState, as a name, describes the fact that the state changes, and whatever the reducer returns should be some change upon the previous state.

Hope this clarifies.

1 Like

Thank you , I appreciate it!