import React, { useReducer, memo } from 'react';
const initialState = 0;
function reducer(state, action) {
switch (action.type) {
case 'reset':
return { count: initialState };
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
}
function Counter(props) {
return (
<div>
Count: {props.state.count}
<button onClick={() => props.dispatch({ type: 'reset' })}>Reset</button>
<button onClick={() => props.dispatch({ type: 'increment' })}>+</button>
<button onClick={() => props.dispatch({ type: 'decrement' })}>-</button>
</div>
);
}
const Detail_ = props => {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<>
<div className="App">
<Counter state={state} dispatch={dispatch} />
</div>
</>
);
};
export const Detail = memo(Detail_);