useState()

sinu·2022년 1월 3일
0

useState : state 를 관리

const [count, setCount] = useState(0);
function onClickBtn() {
  setCounter(counter + 1)
}

onClickBtn 함수가 아무 문제 없이 동작할 것으로 보이지만 count를 변경 할때 원하는 값으로 출력 되지 않는 경우 존재 할 수 있다.

이러한 경우

const [count, setCount] = useState(0);
function onClickBtn() {
  setCounter((current) => (current + 1));
}

와 같이 현재 state 값에 기반하여 업데이트를 해주면 된다.

profile
sinu.log("...")

0개의 댓글