
if (mode === "READ") {
...
}
● state 변경 → 리렌더링
● setMode()가 화면 전환 트리거
● React는 상태 기반 렌더링 구조
const newTopics = [...topics];
newTopics.push(newTopic);
setTopics(newTopics);
● 배열/객체는 참조 타입
● 원본 수정하면 React가 변경 감지 못함
● 새 배열을 만들어 교체해야 리렌더링
const title = event.target.title.value;
const [title, setTitle] = useState("");
<input
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
● input의 값은 DOM이 아니라 React state가 관리
● value + onChange는 세트
● state가 단일 진실 공급원 (Single Source of Truth)
const updatedTopics = topics.map((t) =>
t.id === id ? { ...t, title, body } : t
);
● map으로 교체
● filter로 삭제
● find로 조회
● 내부 state로 화면 제어
● 새로고침하면 상태 초기화
● URL 공유 불가
<Route path="/topics/:topic_id" element={<Topic />} />
● URL이 state 역할
● 새로고침해도 유지
● 뒤로가기 가능
● 북마크 가능
const { topic_id } = useParams();
● URL의 :topic_id 값 추출
● 동적 라우팅 구현
<Outlet />
● 자식 Route가 렌더링될 자리
● 부모 레이아웃 유지 + 내용만 변경
const SimpleButton = styled.button`
color: white;
`;
const LargeButton = styled(SimpleButton)`
font-size: 50px;
`;
const ReactButton = (props) => {
return <button>{props.children}</button>;
};
👉 className을 전달하지 않음
const ReactButton = (props) => {
return <button className={props.className}>{props.children}</button>;
};
<button {...props}>
styled-components는 className을 주입하는 방식이다.
state가 바뀌면 리렌더링
URL이 바뀌면 리렌더링
className을 주입해서 스타일링
React는 결국 상태 변화 기반 렌더링 시스템이다.