I'm sure this is a stupid question...

I have a a situation where I want to take the current state of part of a store, and create a new store based on said state, which won't affect the original store. This is my current attempt:
const [statStore, setStatStore] = createStore(
unwrap(systemStore.stats),
);
const [statStore, setStatStore] = createStore(
unwrap(systemStore.stats),
);
I really thought that the unwrap would detach the data from its parent, but I can see there's still some Symbol(solid-proxy): values going on. Do I need to, like, deeply unwrap the data?
9 Replies
Lumphammer (Neil de Carteret)
Solved it with a JSON.parse(JSON.stringify(x)) as typeof x 🤷‍♂️
Madaxen86
Madaxen86•3w ago
const [store, setStore] = createStore(initial);
const [nested] = createStore({ ...store });
const [store, setStore] = createStore(initial);
const [nested] = createStore({ ...store });
Lumphammer (Neil de Carteret)
That wouldn’t unlink it deeply though, right?
zulu
zulu•3w ago
right, it will be shallow clone and you are interested in a more deep clone ( like the one you have) your deep clone is fine, as long as your store is actually JSON serializable.
Éverton Toffanetto
You can use structuredClone from vanilla JavaScript for deep clones.
zulu
zulu•3w ago
I thought JSON.stringify/parse might be slow, but structuredClone is slower https://www.measurethat.net/Benchmarks/Show/18541
Éverton Toffanetto
The advantage of structuredClone is that it performs deep copies that would otherwise only be possible with libraries or complex manual implementations.
zulu
zulu•3w ago
indeed, additionally it support more complex types like Map , Set Date etc which JSON does not. if you need to retain that it is the better choice.
Lumphammer (Neil de Carteret)
Thank you, somehow I’d missed that

Did you find this page helpful?