State: 컴포넌트의 기억 저장소
일반 함수로 필요한 정보를 모두 저장할 수 없을 때
import { sculptureList } from './data.js';
export default function Gallery() {
//지역변수로 index 변수 생성
let index = 0;
function handleClick() {
//지역변수 값을 변경시키는 함수
index = index + 1;
}
let sculpture = sculptureList[index];
return (
<>
<button onClick={handleClick}>
Next
</button>
<h2>
<i>{sculpture.name} </i>
by {sculpture.artist}
</h2>
<h3>
({index + 1} of {sculptureList.length})
</h3>
<img
src={sculpture.url}
alt={sculpture.alt}
/>
<p>
{sculpture.description}
</p>
</>
);
}
useState()의 사용법
useState()
작동방식
useReducer()와 useState()
컴포넌트에 여러 state 변수 지정하기
컴포넌트 내부에 State
useState의 한계점
Last updated