How to pass props to context reactively?
I have a context that accepts props and then provides the prop values to its consumer components:
Playground link: https://playground.solidjs.com/anonymous/d7a38bbe-fa34-49c9-9d8f-7211839257ad
How do I make the props passing to the context value reactive?
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
6 Replies
You have to make the context values accessors:
https://playground.solidjs.com/anonymous/699f142f-cfce-448e-9fbf-e66b0863391e
What also works if you want to mimic solid's component prop vibe, it to make them getters:
https://playground.solidjs.com/anonymous/49bdd6b3-f641-4d1c-aa1e-7a1ba5aaf12b
Both these solutions make sure that the reactivity of props.value is preserved
Because in your case, props.value only gets accessed once inside the context provider value property but doesn't stay reactive.
I come from a lot of React work and still trying to get used to the new paradigm. Does it mean that when I call the
useContext
, even if the context value changes (new prop is passed), the child component does not get updated? Do I understand it correctly that, there is no concept of component updates in SolidJS, just updates happening within "tracked scopes" (in my case that tracked scopes are the JSX at top level, where I render FormGroup; and the JSX of the TestComponent)?I really suggest going through the tutorial on the solid site. It teaches you all the basics :)
https://www.solidjs.com/tutorial/introduction_basics
Yes, components do not rerun in solidjs. Every reactive state is contained inside a function or a getter which makes the scope subscribe to the signals it depends on when called.
I actually did go through the tutorial before I started my project with SolidJS but maybe it is time to revisit it again 🙂
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
And maybe have a look at the existing form libraries:
https://modularforms.dev/solid
Or
https://felte.dev/docs/solid/getting-started
They provide easy interfaces to interact with inputs, form validation also adapters for validation libs like zod.
Felte | Getting started | Solid
An extensible form library for Svelte, Solid and React.