#How to initialize a store from another store?

8 messages · Page 1 of 1 (latest)

ashen moat
pine latch
#

use JSON.parse(JSON.stringify(v)), structuredClone or anything that clones deeply

ashen moat
#

oh it's that problem again, alright 😄 thank you!

#

@pine latch would you know why unwrap was not sufficient here?

pine latch
#

unwrap only unwraps the proxy

#

and store modifies the object

#

so you can't really use the same object for multiple stores without deep-cloning

#
const foo = {}, bar = { foo };
const [store, setStore] = createStore(bar);
store // => proxied `bar`
store.foo // => proxied `foo`
unwrap(store) === bar
unwrap(store.foo) === foo

const baz = {}
setStore('foo', { baz }) // { foo: { ...{ baz } } }
foo.baz === baz
unwrap(store.foo.baz) === baz
unwrap(store.foo) === foo