Context is not defined inside context provider #3749
-
I'm building a router and encountered the following bug: const DEFAULT_STATE = {} // Not really relevant
const RouterContext = createContext<routerContext | null>(null)
const Router = ({Wrapper, children}) => {
const [route, setRoute] = useReducer(RouterReducer, DEFAULT_STATE)
// Some stuff missing
return (
<RouterContext.Provider value={{ route, setRoute }}>
<Wrapper>
{ children }
</Wrapper>
</RouterContext.Provider>
)
} Everything works on dev env, but when I transpile it and run it, the Wrapper component cannot access the context to the router, useContext(RouterContext) return null/undefined Note: I'm using vite 3.0.0 for building |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I can't reproduce the error on my end. The router context value is defined and not null. Can you share a reproduction case where we can reproduce the error? |
Beta Was this translation helpful? Give feedback.
-
Thanks for replying. It's stored on here ** Works **
** Doesn't Work **
|
Beta Was this translation helpful? Give feedback.
-
So I found the culprit. It looks like you're developing on a mac and on macOS the file system is case insensitive by default. Meaning In import { ExampleRoute } from "./routes/ExampleRoute";
import MainContainer from "./components/MainContainer";
- import { Router, RouteComponent } from "./context/router";
+ import { Router, RouteComponent } from "./context/Router";
import "./assets/globals.scss"; So what ends up happening is that vite looks for There is already an issue for that on the vite issue tracker: vitejs/vite#964 |
Beta Was this translation helpful? Give feedback.
So I found the culprit. It looks like you're developing on a mac and on macOS the file system is case insensitive by default. Meaning
foo.ts
is treated the same asFoo.ts
. Butvite
is able to detect that and treats both files as a separate module. The thing is that in the repo provided there is no duplicate file, but rather a casing issue in imports.In
./src/appWrapper.jsx
, pay close attention to the casing:So what ends …