Subscribe to any unknown store change
Hi. Is there a way to subscribe to a store change in unknown property?
I have a store:
And sometimes I modify it
And I want to render it's unwrapped value on each change (and I don't know the change upfront)
How to make the store value reactive to unknown changes?
22 Replies
this is currently not possible, the advice given is to wrap setStore instead. Something like
Thanks!
If I want to subscribe to unknown keys I think it's better to solve it by wrapping the store value in some object.
Meaning:
Instead of
Doing
Then I can do
Is it considered a best practice?
I don't think it makes a difference. This strategy of
JSON.stringify
ing the store to trigger an effect will work, but there is a cost to JSON.stringify
.
It's not the most optimal way, the most optimal way rn would be to wrap the setter instead.
as an aside, you do not need unwrap either. JSON.stringify(store)
will be equivalent to JSON.stringify(unwrap(store))
afaik.I try my suggestion - and it did not work
I wonder why
if u could make a minimal reproduction in the playground, i can look at it
Yes
solidjs reactivity - context reactive - store setter (forked)
CodeSandbox is an online editor tailored for web applications.
something specific u have questions about?
Yes - How make the
<div>{JSON.stringify(unwrap(store.value))}</div>
reactive
I mean - I want it to change
JSON.strinfigy(unwrap(store.value))
when value.a changes without knowing upfront that value.a changed (value["unknown"
)ok
don't unwrap
i guess unwrap untracks the reactive value
You right!
Thank you!
yes, i don't think u ever really need to use unwrap tbh
u can just work w the proxy directly
ur welcome!
Actually this seems resonable (unwrap inside effect does not track the dependency)
a didn't know that, never really touched it. only use it if i want cleaner debug-log
to get rid of the proxy-clutter.
there is also this thing: https://codesandbox.io/s/observing-state-6epzp?file=/index.js
ryansolid
CodeSandbox
Observing State - CodeSandbox
Observing State by ryansolid using babel-preset-solid, object-observer, solid-js
Thanks!
Now I created another demo that confused me a bit
Is JSON.stringify(store) reactive? Not that I removed the unwrap (my mistake for it sorry) it seems that store value is updated
@bigmistqke @thetarnav
Sorry - my mistake - store is reactive. Meaning that if store.a changes - store changes too so it's jsx / effect will rerun.
I infer it by looking at this codesandbox I created:
https://codesandbox.io/p/sandbox/solidjs-reactivity-context-reactive-store-setter-eccv7z?file=%2Fsrc%2FApp.tsx&selection=%5B%7B%22endColumn%22%3A10%2C%22endLineNumber%22%3A16%2C%22startColumn%22%3A10%2C%22startLineNumber%22%3A16%7D%5D
solidjs reactivity - context reactive - store setter
CodeSandbox is an online editor tailored for web applications.
that's neat
what is the question?
No question. Just clarification to myself and other that will look at this in the future
Thanks for the help 🙂
ur welcome 👍
By any chance would you know how to correctly type the aliased
setStore
with Typescript?
When typed as below, the type of the args is never
, which throws errors in the existing setters,
there is
StoreSetter
from solid-js/store