useReducer
const INITIAL_STATE = { name: '' }; // 초기 상태값
const reducer = (state, action) => {
switch (action.type) {
case 'setName':
return {
...state,
name: action.name,
};
}
};
function App() {
const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
const onChange = (e) => {
dispatch({ type: 'setName', name: e.target.value });
};
return (
<div className="App">
<input type="text" value={state.name} onChange={onChange} />
</div>
);
}
Last updated