This commit is contained in:
Ruidy 2021-04-05 10:22:06 +02:00
parent d5668eb537
commit 9f7743c8f1
3 changed files with 7 additions and 13 deletions

View file

@ -1,2 +0,0 @@
export type Action = { type: "fetchMeal" | "fetchRandomMeal" | "toggleFav" };
export type Dispatch = (action: Action) => void;

View file

@ -2,18 +2,12 @@
import { createContext, FC, useContext, useReducer } from "react"; import { createContext, FC, useContext, useReducer } from "react";
import { MealApi } from "../../types/meal"; import { MealApi } from "../../types/meal";
import { Dispatch } from "./actions"; import { appReducer, Dispatch } from "./reducer";
import { appReducer } from "./reducer";
const AppContext = createContext<
| {
state: AppState;
dispatch: Dispatch;
}
| undefined
>(undefined);
export type AppState = { meals: MealApi[] }; export type AppState = { meals: MealApi[] };
type ContextType = { state: AppState; dispatch: Dispatch } | undefined;
const AppContext = createContext<ContextType>(undefined);
export const useMeal = () => { export const useMeal = () => {
const context = useContext(AppContext); const context = useContext(AppContext);

View file

@ -1,4 +1,3 @@
import { Action } from "./actions";
import { AppState } from "./index"; import { AppState } from "./index";
export const appReducer = (state: AppState, action: Action) => { export const appReducer = (state: AppState, action: Action) => {
@ -14,3 +13,6 @@ export const appReducer = (state: AppState, action: Action) => {
} }
} }
}; };
export type Action = { type: "fetchMeal" | "fetchRandomMeal" | "toggleFav" };
export type Dispatch = (action: Action) => void;