contextProvider using signal
Hi, noob question here -- just getting feet wet w/ SolidStart. Any help is appreciated!!
Trying to create a context provider for setting theme, and prop is getting passed down but child comps not reacting.
(cont'd)
16 Replies
Then boiler plate root.tsx, wrapped in ThemeProvider. The theme is getting passed and UI reflects the change.
However, if I call the setter with a different theme, the UI is not reacting to the new value. I'm sure the setter is getting because value updates from within
theme-provider.tsx
.
This is so basic that I'm clearly doing something wrong, but I'm two hours in and making no progress -- hoping somebody can shed some light.value={[theme(), setTheme]}
This one is basically a mistake. Context Provider's value
prop isn't reactive so what you pass here will never change. It's better if you pass theme
as-is
In this case instead of string
it would be () => string
so value={[theme, setTheme]}
Thanks you so much for the quick response! Let me give a try and let it sink in -- I'll get back -- Super appreciate help!!!
@lxsmnsyc , ty! that did the trick (jsut needed to fix the type error since accessor instead of the val). Diff issue though, inner components are updating (I put test elem in body reflecting theme val), but data-theme attrib on <html> tag val not reacting to state change. any thought there? Sorry to have to ask 😕 but you got me so close now.
might be getting the default context value, which is a bug in HMR. Have you tried refreshing the page?
y, refreshed. no luck
ah you meant the html
y
I'm not sure if html attributes are reactive though
also try calling it
theme()
still no luck
what does your root look like now?
also you're calling
const [theme, setTheme] = useTheme();
outside ThemeProvider
?let me do experiment to see if html reactive or now -- will get back in a few -- ty!
calling it outside means
useTheme
will always return the default context valueverified html el is reacting ok w/ some test code.
This is really odd, because in index.tsx, same setup and calling pattern, but works
@hotshoe
the flow is wrong
Agggggggh!!!!!
Ty!! Sorry for the bother., but super appreciated!!
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View