React 버전 16.8부터 React 요소로 새로 추가됐다.

함수형 컴포넌트에서 상태를 관리할 수 있게 됐다.

useState는 어떻게 동작하는가?

기본적인 useState의 구조

function useState(initialValue) {
	var _val = initialiValue
	function state() {
		return _val
	}
	function setState(newVal) {
		_val = newVal
	}
	return [state, setState]
}

useState 를 사용할 때 보통 아래와 같은 형태로 사용한다.

const [state, setState] = useState(0);

setState 를 통해 state의 값을 변경하고, 그 값이 계속 남아있어 상태를 관리할 수 있게 된다.

실제 react의 useState

Untitled

Untitled