1) Props Drilling 해결하여 가독성 향상
2) 여러 컴포넌트에서 상태 공유
3) 일관된 상태 관리 가능
4) 코드 유지보수성 향상
useState, useReducer, useContext
useState : 컴포넌트 내부에서 로컬 상태를 관리하는 기본적인 방법useReducer : 복잡한 상태 로직을 다룰 때 사용하며, Redux와 유사한 패턴을 가짐useContext : 전역적으로 상태를 공유하는 방법으로, 여러 컴포넌트에서 동일한 데이터를 접근할 수 있도록 함context API
Redux : Flux 아키텍처 기반의 상태 관리 라이브러리로, 단일 상태 저장소(Store)를 통해 상태를 중앙에서 관리Redux Toolkit : Redux의 사용성을 개선한 공식 라이브러리로, 보일러플레이트(반복 코드)를 줄이고 효율적인 상태 관리를 제공컴포넌트 트리에서 최상위 컴포넌트의 데이터를 하위 컴포넌트로 전달할 때, 여러 단계를 거쳐야 하는 현상
React로 개발하다 보면 발생하는 자연스러운 현상
import { useState } from "react";
import styled from "styled-components";
const Component = styled.div`
font-weight: 700; border: 3px solid blue; border-radius: 10px; flex-grow: 1;
line-height: 30px; text-align: center; padding: 10px; margin: 10px;
> button {
margin-left: 10px;
}
`;
const Container = styled.div`
display: flex; width: 100%; justify-content: center;
`;
export default function App() {
const [count, setCount] = useState(0);
console.log("App");
return (
<Container>
<Component>
App
<Container>
<Child1 count={count}/>
<Child2 count={count} setCount={setCount} />
</Container>
</Component>
</Container>
);
}
function Child1({count}) {
console.log("Child1");
return (
<Component>
Child1
<Container>
<Child3 count={count}/>
<Child4 />
</Container>
</Component>
);
}
function Child2({count, setCount}) {
console.log("Child2");
return (
<Component>
Child2
<Container>
<Child5 />
<Child6 count={count} setCount={setCount} />
</Container>
</Component>
);
}
function Child3({count}) {
console.log("Child3");
return <Component>Child3 : {count}</Component>;
}
function Child4() {
console.log("Child4");
return <Component>Child4</Component>;
}
function Child5() {
console.log("Child5");
return <Component>Child5</Component>;
}
function Child6({count, setCount}) {
console.log("Child6");
return (
<Component>
Child6
<button onClick={() => setCount(count + 1)}>+</button>
</Component>
);
}

<Child3\> 컴포넌트에서 <App\> 컴포넌트의 count 를 받아오려면 App → Child1 → Child3 순서로 전달<Child6\> 컴포넌트에서 <App\> 컴포넌트의 count, setCount 를 받아오려면 App → Child2 → Child6 순서로 전달<Child1\> , <Child2\> 는 해당 상태를 직접 사용하지 않지만, 단순히 props를 넘겨주는 역할만 수행
