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)
Trying to create a context provider for setting theme, and prop is getting passed down but child comps not reacting.
// theme-provider.tsx
import {
createContext,
useContext,
ParentComponent,
createSignal,
} from "solid-js";
const defaultTheme = "light";
export type ThemeContextValue = [get: string, set: (theme: string) => void];
const ThemeContext = createContext<ThemeContextValue>([
defaultTheme,
(theme: string) => {},
]);
export const ThemeProvider: ParentComponent<{
theme?: string;
}> = (props) => {
const [theme, setter] = createSignal(defaultTheme);
const setTheme = (val: string) => {
console.log("ThemeProvider: setTheme", val);
setter(val);
};
return (
// Note: if I put el here and bind to theme value, // it is reactive when called using setter from // child comp, so seems everything is working here.
<ThemeContext.Provider value={[theme(), setTheme]}>
{props.children}
</ThemeContext.Provider>
);
};
export const useTheme = () => useContext(ThemeContext);// theme-provider.tsx
import {
createContext,
useContext,
ParentComponent,
createSignal,
} from "solid-js";
const defaultTheme = "light";
export type ThemeContextValue = [get: string, set: (theme: string) => void];
const ThemeContext = createContext<ThemeContextValue>([
defaultTheme,
(theme: string) => {},
]);
export const ThemeProvider: ParentComponent<{
theme?: string;
}> = (props) => {
const [theme, setter] = createSignal(defaultTheme);
const setTheme = (val: string) => {
console.log("ThemeProvider: setTheme", val);
setter(val);
};
return (
// Note: if I put el here and bind to theme value, // it is reactive when called using setter from // child comp, so seems everything is working here.
<ThemeContext.Provider value={[theme(), setTheme]}>
{props.children}
</ThemeContext.Provider>
);
};
export const useTheme = () => useContext(ThemeContext);(cont'd)