Posted originally in General.
Does anyone know why a context provider may not have a value ready despite definitely being within a provider? Maybe a question for the help forum but the context looks sort of like:
import { useReducer, createContext, useContext } from "react";
import { ReduceFunc } from "reducer";
import type { ReducerState, ReducerActions } from "reducer";
import type { ReactNode, Dispatch } from "react";
type ReducerContextType = [ReducerState, Dispatch<ReducerActions>];
const ReducerContext = createContext<ReducerContextType | undefined>(undefined);
function ReducerProvider({ children }: { children: ReactNode }) {
const [state, dispatch] = useReducer(ReduceFunc, {/*default state*/});
return <ReducerContext.Provider value={[state, dispatch]}>{children}</ReducerContext.Provider>;
}
function useRootReducer() {
const context = useContext(ReducerContext);
if (context === undefined) {
throw new Error("useSidebar must be used within a SidebarProvider");
}
return context;
}
export { ReducerProvider, useRootReducer };