Created
June 2, 2020 10:59
-
-
Save muddassirm/674398e428b08e069cb07cf1def7dab7 to your computer and use it in GitHub Desktop.
A a very simple immer alternative
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
let state = { | |
lists: [1, 2, 3, 4], | |
items: ['a', 'b', 'c'], | |
} | |
const cloneMyState = (f) => { | |
let copy = { | |
items: [...state.items], | |
lists: [...state.lists] | |
} | |
return f(copy) | |
} | |
let update = cloneMyState(draft=>{ | |
draft.items.push('d') | |
draft.lists.splice(1,1) | |
return draft | |
}) | |
console.log(update, update.items == state.items,update.lists == state.lists) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment