Skip to content

Instantly share code, notes, and snippets.

@iskenxan
Created May 23, 2021 17:57
Show Gist options
  • Save iskenxan/7f8d93ec6875c611f0633a7a8d76d223 to your computer and use it in GitHub Desktop.
Save iskenxan/7f8d93ec6875c611f0633a7a8d76d223 to your computer and use it in GitHub Desktop.
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