useReducer
useReducer
useReducer 사용법
const [state, dispatch] = React.useReducer(함수, {기본값});예제 - useState를 사용한 달풍선 충전개수 선택 화면
function MainComponent(){
const [total, setTotal] = React.useState(0);
const minusOne = e=>{
if(total < 1) return;
setTotal(total - 1);
};
const minusTen = e=>{
if(total < 10) {
setTotal(0);
}
else {
setTotal(total - 10);
}
};
const plusOne = e=>{
setTotal(total + 1);
};
const plusTen = e=>{
setTotal(total + 10);
}
return (
<>
<h1>충전할 달풍선 개수를 선택하세요</h1>
<button onClick={minusTen}>-10개</button>
<button onClick={minusOne}>-1개</button>
<button onClick={plusOne}>+1개</button>
<button onClick={plusTen}>+10개</button>
<hr/>
<h2>충전할 총 달풍선 개수 : {total}개</h2>
</>
);
}
const app = ReactDOM.createRoot(document.querySelector("#app"));
app.render(<MainComponent/>);
예제 - useReducer를 사용한 달풍선 충전개수 선택 화면

예제 - 회원 가입 화면

예제 - 회원 가입 화면과 정규표현식 검사
Last updated