Skip to content

Instantly share code, notes, and snippets.

@muddassirm
Created June 2, 2020 10:59
Show Gist options
  • Save muddassirm/674398e428b08e069cb07cf1def7dab7 to your computer and use it in GitHub Desktop.
Save muddassirm/674398e428b08e069cb07cf1def7dab7 to your computer and use it in GitHub Desktop.
A a very simple immer alternative
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