Solid js context turns out undefined. What's happening? #2564
Replies: 2 comments
-
|
I found a solution to the problem... Move the context creation to a new file: PaletteCreateContext.tsx Import PaletteCreateContext into the context file.
... That makes the useContext work again without having to reload it every time the HMR (Hot Module Replacement) is fired. |
Beta Was this translation helpful? Give feedback.
-
Solution: SolidJS Context Undefined on HMRThis is a known issue with HMR (Hot Module Replacement) in SolidJS. When you save a file, HMR recreates components but the context reference becomes stale. Fix 1: Add default value to createContext// PaletteContext.tsx
export const PaletteContext = createContext<{
color: typeof initialColor;
setColor: SetStoreFunction<typeof initialColor>;
} | undefined>(undefined);Fix 2: Create context with proper typingimport { createContext, useContext, ParentComponent } from "solid-js";
import { createStore, SetStoreFunction } from "solid-js/store";
type PaletteContextType = {
color: ColorStore;
setColor: SetStoreFunction<ColorStore>;
};
const PaletteContext = createContext<PaletteContextType>();
export const PaletteContextProvider: ParentComponent = (props) => {
const [color, setColor] = createStore(initialValue);
return (
<PaletteContext.Provider value={{ color, setColor }}>
{props.children}
</PaletteContext.Provider>
);
};
export const usePaletteContext = () => {
const context = useContext(PaletteContext);
if (!context) {
throw new Error("usePaletteContext must be used within PaletteContextProvider");
}
return context;
};Fix 3: Vite config for better HMR// vite.config.ts
export default defineConfig({
plugins: [solid()],
server: {
hmr: {
overlay: false,
},
},
});Root CauseWhen HMR updates a module, it creates a NEW context reference. Components using the old reference get undefined. The fixes above ensure proper error handling and context stability. Also check that you do not have duplicate imports of the context file with different paths (case sensitivity matters). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on a project in Solid-js. Until a few days ago it worked fine…
But suddenly it gives me the following error:
ColorEditOpacityValue.tsx:9 Uncaught (in promise) TypeError: Cannot destructure property 'color' of 'usePaletteContext(...)' as it is undefined.Everytime I save a file in development mode!
I have 3 context files surrounding the APP component and as far as I can see it’s only the usePaletteContext(...) that becomes undefined.
In the index.tsx file the context providers are wrapped like this
Every component that needs the context is setup as follows
The following is the context file in its self:
As I only see the problem with the
usePaletteContextfile I was wondering if the file is getting too big?Or else how come it suddenly starts giving me problems and what can be done to solve it?
I have also looked into the Development HMR (Hot Module Replacement) issues, but I can’t find any way to solve it if that’s the problem…
/Bo
Beta Was this translation helpful? Give feedback.
All reactions