
// This is a React Quiz from BFE.dev
import * as React from 'react'
import { useState, useEffect } from 'react'
import { createRoot } from 'react-dom/client'
function App() {
const [state1, setState1] = useState(1);
const [state2] = useState(() => {
console.log(2);
return 2;
});
console.log(state1);
useEffect(() => {
setState1(3);
}, []);
return null;
}
const root = createRoot(document.getElementById("root"));
root.render(<App />);
2
1
3
2 출력: useState(() => { ... })의 형태는 Lazy Initial state로 비용이 큰 연산의 경우일 때 사용합니다. 컴포넌트가 처음 마운트될 때만 실행하여 초기값을 결정합니다.
1 출력: state1는 1로 초기화되어 출력됩니다.
3 출력: setState에 의해 state1는 3로 출력됩니다.