Send Action Data to the Store- Redux

Tell us what’s happening:

Your code so far


const ADD_NOTE = 'ADD_NOTE';

const notesReducer = (state = 'Initial State', action) => {
  switch(action.type) {
    // change code below this line
     case ADD_NOTE:
      return ({
        state:addNoteText(text)
      });
      break;
    // change code above this line
    default:
      return state;
  }
};

const addNoteText = (note) => {
  // change code below this line
return({
  type: ADD_NOTE,
  text:note
});
  // change code above this line
};

const store = Redux.createStore(notesReducer);

console.log(store.getState());
store.dispatch(addNoteText('Hello!'));
console.log(store.getState());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/send-action-data-to-the-store

@nidhisharma Two things:

#1

If you are using the return statement, breack is not required. This is more concise only with return.

#2

No mutate the state. Principle of Redux. ¿What object must return you? ¿keys? ¿values of each keys?

3 Likes

Thanka a lot Yoelvis…

1 Like

@nidhisharma & @yoelvis,

You are right @yoelvis that a principle of Redux is not mutating state. Earlier in the Redux section, they explain that we are not worrying about making non-mutating changes to state yet. In this challenge state is not an object but a string. To make a change to this state without mutating we would use the concat() method. But the state is not initialized yet.

(state = 'Initial State', action)

This is a default value syntax that is used here. Because the state is not assigned any value anywhere else I think that the state will only be assigned this default value if the switch statement runs and a value is not assigned to it.

So I think that we are not mutating state in the exercise. But I am curious because it definitely felt like I was mutating state when I first completed this challenge.

Thank you for you explanation. This problem was making me increasingly frustrated, but tamed it by asking myself the questions you posted. Thanks :smiley:

1 Like