Skip to content

Instantly share code, notes, and snippets.

@Nilanth

Nilanth/.jsx Secret

Created July 12, 2021 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nilanth/712cb51deafdbe9d1683a8bb10cabed0 to your computer and use it in GitHub Desktop.
Save Nilanth/712cb51deafdbe9d1683a8bb10cabed0 to your computer and use it in GitHub Desktop.
immer redux
import produce from "immer"
// Reducer with initial state
const INITIAL_STATE = [
/* bunch of todos */
]
const todosReducer = produce((draft, action) => {
switch (action.type) {
case "toggle":
const todo = draft.find(todo => todo.id === action.id)
todo.done = !todo.done
break
case "add":
draft.push({
id: "todo_" + Math.random(),
title: "A new todo",
done: false
})
break
default:
break
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment