How does the reducer function knows that todo object is coming from the action creator addToDo?

Tell us what’s happening:

Hey,

I’m a bit confused how the action.todo gets pushed correctly in the reducer function. I can’t track down where the reducer function is referring to that particular action creator.

Your code so far


const immutableReducer = (state = ['Do not mutate state!'], action) => {
switch(action.type) {
  case 'ADD_TO_DO':
    // don't mutate state here or the tests will fail
    return [...state, action.todo];
  default:
    return state;
}
};

const addToDo = (todo) => {
return {
  type: 'ADD_TO_DO',
  todo
}
}

const store = Redux.createStore(immutableReducer);

Your browser information:

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

Challenge: Use the Spread Operator on Arrays

Link to the challenge:

If the action object’s type property has a value of ADD_TO_DO, add the to-do to the state.


const addToDo = (todo) => {
  return {
    type: 'ADD_TO_DO',
    todo
  }
}

It looks very much like that’s going to be an object with the type of ADD_TO_DO.

1 Like