import { createContext, useContext, useReducer } from 'react'; type State = { count: number }; type Action = { type: 'inc' } | { type: 'reset' }; const CounterContext = createContext<{state:State;dispatch:React.Dispatch} | null>(null); function reducer(state: State, action: Action): State { return action.type === 'inc' ? {count:state.count+1} : {count:0}; } export function CounterProvider({children}:{children:React.ReactNode}) { const [state,dispatch]=useReducer(reducer,{count:0}); return {children}; } export function useCounter(){ const value=useContext(CounterContext); if(!value) throw new Error('CounterProvider missing'); return value; }