-
-
Save Nilanth/712cb51deafdbe9d1683a8bb10cabed0 to your computer and use it in GitHub Desktop.
immer redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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