import React from "react";
import { useReducer } from "react";
const initialState = {
count: 0,
};
type ReducerProps = {
state: any;
};
const reducer = (state: state, action) => {
switch (action.type) {
case "increment":
return { count: state.count + action.payload };
case "decrement":
return { count: state.count - action.payload };
default:
return state;
}
};
const Counter = () => {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
Count : {state.count}
<button
onClick={() => {
dispatch({ type: "increment", payload: 1 });
}}
>
Increment ++
</button>
<button
onClick={() => {
dispatch({ type: "decrement", payload: 1 });
}}
>
Decrement --
</button>
</div>
);
};
export default Counter;