본문으로 건너뛰기

Props와 state

질문

React에서 props와 state는 어떻게 다른가요?

답변 초안

props는 부모가 내려주는 입력값이고, state는 component 안에서 시간에 따라 바뀌는 값입니다.

예시

type CounterProps = {
label: string;
};

function Counter({label}: CounterProps) {
const [count, setCount] = useState(0);

return (
<button onClick={() => setCount(count + 1)}>
{label}: {count}
</button>
);
}

실무 주의점

  • 여러 component가 같은 값을 필요로 하면 state 위치를 위로 올립니다.
  • 서버에서 온 데이터는 local state와 server state를 구분해서 관리합니다.

참고 자료

  • TBD