import { useReducer, useContext, createContext } from 'react' const CounterStateContext = createContext() const CounterDispatchContext = createContext() const reducer = (state, action) => { switch (action.type) { case 'INCREASE': return state + 1 case 'DECREASE': return state - 1 case 'INCREASE_BY': return state + action.payload default: throw new Error(`Unknown action: ${action.type}`) } } export const CounterProvider = ({ children }) => { const [state, dispatch] = useReducer(reducer, 0) return ( {children} ) } export const useCount = () => useContext(CounterStateContext) export const useDispatchCount = () => useContext(CounterDispatchContext)