import React, { render, useReducer } from "react";
type State = {
word: string,
count: number
}
type Action =
| { type: 'WORD', value: string }
| { type: 'DELETE' }
| { type: 'RESET' }
const reducer = (state: State, action: Action) => {
switch (action.type) {
case "WORD":
return {
count: action.value.length,
word: action.value
};
case "DELETE":
return state.word.length
? {
count: state.count - 1,
word: state.word.slice(0, -1)
}
: state;
case "RESET":
return { word: '', count: 0 };
default:
throw new Error("Action not handled");
}
};
const CounterWithReducer = () => {
const [state, dispatch] = useReducer(
reducer,
{ count: 0, word: "" }
);
return (
<div>
<h1>Counter with useState</h1>
<p>Letters: {state.count}</p>
<input
value={state.word}
onChange={(e) => {
dispatch({
type: 'WORD',
value: e.currentTarget.value
})
}}
/>
<button
onClick={() => dispatch({ type: 'DELETE' })}>
Delete
</button>
<button
onClick={() => dispatch({ type: 'RESET' })}>
Reset
</button>
</div>
);
};
render(<CounterWithReducer />)
body {
padding: 16px;
font-size: 24px;
}
h1 {
font-size: 32px;
}