Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Created January 9, 2018 08:19
Show Gist options
  • Save mweststrate/7d6c6fe7748486bf137839b7db876402 to your computer and use it in GitHub Desktop.
Save mweststrate/7d6c6fe7748486bf137839b7db876402 to your computer and use it in GitHub Desktop.
immer-producer-2
import produce from "immer"
const todos = [ /* 2 todo objects in here */ ]
const nextTodos = produce(todos, draft => {
draft.push({ text: "learn immer", done: true })
draft[1].done = true
})
// old state is unmodified
console.log(todos.length) // 2
console.log(todos[1].done) // false
// new state reflects the draft
console.log(nextTodos.length) // 3
console.log(nextTodos[1].done) // true
// structural sharing
console.log(todos === nextTodos) // false
console.log(todos[0] === nextTodos[0]) // true
console.log(todos[1] === nextTodos[1]) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment