Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 1.24 KB

7 Avoiding Object Mutations.md

File metadata and controls

54 lines (43 loc) · 1.24 KB
< Previous abstract Back To React Folder Next abstract >

Avoiding Object Mutations

With .assign()

const testToggleTodos = () => {
  const todoBefore = {
    id: 0,
    text: "Learn Redux",
    isCompleted: false
  };
  const todoAfter = {
    id: 0,
    text: "Learn Redux",
    isCompleted: true
  };
};

//This func
const toggleTodos = todo => {
  todo.isCompleted = !todo.isCompleted;
  return todo;
};

And this is bad, cuz it tries to mutate our Object;

Simpliest solution would be:

const toggleTodos = todo => {
  return {
    id: todo.id,
    text: todo.text,
    isCompleted: !todo.isCompleted
  };
};

Yet we can forget to update that from time to time. This is why there are a nice ES6 method called Object.assign()

const toggleTodos = todo => {
  return Object.assign({}, todo, {
    isCompleted: !todo.isCompleted
  });
};

Perfect!