mirror of
https://github.com/rjNemo/meal_planner
synced 2026-06-12 13:26:45 +00:00
35 lines
989 B
TypeScript
35 lines
989 B
TypeScript
//https://kentcdodds.com/blog/how-to-use-react-context-effectively
|
|
|
|
import { createContext, FC, useContext, useReducer } from "react";
|
|
import { MealApi, MealSummary } from "../../types/meal";
|
|
import { appReducer, Dispatch } from "./reducer";
|
|
|
|
export type AppState = {
|
|
meals: MealApi[];
|
|
search: MealSummary[];
|
|
searchString: string;
|
|
};
|
|
|
|
const initState = {
|
|
meals: [] as MealApi[],
|
|
search: [] as MealSummary[],
|
|
searchString: "",
|
|
};
|
|
|
|
type ContextType = { state: AppState; dispatch: Dispatch } | undefined;
|
|
|
|
const AppContext = createContext<ContextType>(undefined);
|
|
|
|
export const useMeal = () => {
|
|
const context = useContext(AppContext);
|
|
if (!context) {
|
|
throw new Error("useMeal must be used within a AppProvider");
|
|
}
|
|
return context;
|
|
};
|
|
|
|
export const AppProvider: FC = ({ children }) => {
|
|
const [state, dispatch] = useReducer(appReducer, initState);
|
|
const value = { state, dispatch };
|
|
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
|
|
};
|