"use client"; import { PropsWithChildren, createContext, useReducer // , useState } from "react"; // interface IFavoriteContext { // favorites: Array // setFavorites: (value: Array) => void // } // export const FavoriteContext = createContext({ // favorites: [], // setFavorites: (_value: Array) => { }, // }); // // Using interleaving to render this context provider client-side, but still utilize server-side renderung for children components // export const FavoriteProvider = ({ children }: PropsWithChildren) => { // const [favorites, setFavorites] = useState>([]) // return ( // {children} // ) // } interface IFavoriteContext { favorites: FavoriteState; dispatch: React.Dispatch; } type FavoriteState = Array; type FavoriteAction = { type: "Add" | "Remove"; payload: IMovieSearch }; function FavoriteReducer(state: FavoriteState, action: FavoriteAction) { switch (action.type) { case "Add": state = [...state, action.payload]; break; case "Remove": state = state.filter((s) => s.imdbID != action.payload.imdbID); default: break; } return state; } const initialState: FavoriteState = []; export const FavoriteContext = createContext({ favorites: initialState, dispatch: () => null, }); export function FavoriteProvider({ children }: PropsWithChildren) { const [favorites, dispatch] = useReducer(FavoriteReducer, initialState); return ( {children} ); }