Created
May 23, 2021 17:57
-
-
Save iskenxan/7f8d93ec6875c611f0633a7a8d76d223 to your computer and use it in GitHub Desktop.
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, { enablePatches } from "immer"; | |
// some previous code is ommited for brevity | |
enablePatches(); | |
let inverseChanges = []; | |
export const reducer = (state, action) => { | |
return produce( | |
state, | |
(draft) => { | |
switch (action.type) { | |
case OPEN_DOOR: { | |
const { index } = action.payload; | |
draft.doors[index].isOpen = true; | |
break; | |
} | |
case RESET: { | |
draft.doors = generateDoors(); | |
inverseChanges = []; | |
break; | |
} | |
default: | |
break; | |
} | |
}, | |
(patches, inversePatches) => { | |
if (action.type === OPEN_DOOR) { | |
inverseChanges.push(...inversePatches); | |
} | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment