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:
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
Solved it with a
JSON.parse(JSON.stringify(x)) as typeof x
🤷‍♂️That wouldn’t unlink it deeply though, right?
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.
You can use structuredClone from vanilla JavaScript for deep clones.
I thought JSON.stringify/parse might be slow, but
structuredClone
is slower
https://www.measurethat.net/Benchmarks/Show/18541The advantage of structuredClone is that it performs deep copies that would otherwise only be possible with libraries or complex manual implementations.
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.
Thank you, somehow I’d missed that