S
SolidJS•16mo ago
Mathieu

How to initialize a store from another store?

I'd like to initialize a store (say store2) from another store (say store1). However if I change store2, I don't want it to affect store1. In the example below, I tried using unwrap but that didn't help: when a value in store2 changes, it affects store1. https://playground.solidjs.com/anonymous/b623bc24-5945-4676-a5cd-708584f6a2cc
4 Replies
mdynnl
mdynnl•16mo ago
use JSON.parse(JSON.stringify(v)), structuredClone or anything that clones deeply
Mathieu
MathieuOP•16mo ago
oh it's that problem again, alright 😄 thank you! @mdynnl would you know why unwrap was not sufficient here?
mdynnl
mdynnl•16mo ago
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
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
vibeman1987
vibeman1987•16mo ago
let store1_noProxy = Object.assign(store1)
let store1_noProxy = Object.assign(store1)
Want results from more Discord servers?
Add your server