const initialState = { count: 0 };
function reducer(state, action) {
console.log('Current state:', state);
console.log('Action received:', action);
switch (action.type) {
case 'INCREMENT':
return { ...state, count: state.count + (action.payload ?? 0) };
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
const handleClick = () => {
dispatch({ type: 'INCREMENT', payload: 5 }); // This object goes to reducer as 'action'
};
return <button onClick={handleClick}>Count: {state.count}</button>;
}