HELP on Never Mutate State

**I’ve assigned to state2 the state in order to avoid mutating the state array, and therefore making a _push _ on the state2 which is an array. But nothing worked. what to do Please??? state2 is supposed to be a copy of state **

Your code so far


const ADD_TO_DO = 'ADD_TO_DO';

// A list of strings representing tasks to do:
const todos = [
  'Go to the store',
  'Clean the house',
  'Cook dinner',
  'Learn to code',
];

const immutableReducer = (state = todos, action) => {
  let state2 = todos;
  switch(action.type) {
    case ADD_TO_DO:
      // don't mutate state here or the tests will fail
      {
      state2.push(action.todo);
       return state2;
      }
    default:
      return state;
  }
};

// an example todo argument would be 'Learn React',
const addToDo = (todo) => {
  return {
    type: ADD_TO_DO,
    todo: 'Learn React'
  }
}

const store = Redux.createStore(immutableReducer);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/never-mutate-state

Assigning an array to another variable is still pointing a reference it, so you are actually mutating its state.

Search google on how to make a copy of an array. You can use slice method or map method as well.

You should also assign paratmeter todo to todo here.

Hello brother.

You instead of mapping through the array or slicing it you could try using CONCAT in order to place the ACTION.TODO on the last position of the array.

I hope that helps a little bit.

Peace!

Thanks @luizmelo001 it’s ok now. I’m actually struggling with the 2nd project on markdown previewer. Please Hey guy, Thanks a lot. It worked! Now how to parse a markdown code to an html? I’ve used the new Remarkable() object, then called the render method with no success. It’s said on the http:localhost:3000 page Remarkable is not derfined . What to do please?

const md = new Remarkable();
...
<div id="preview" className="previewer" dangerouslySetInnerHTML = {{__html: md.render(this.state.markdown)}}/>