1️⃣ React Context는 무엇인가?


https://react.dev/learn/passing-data-deeply-with-context

“Passing Data Deeply with Context” Usually, you will pass information from a parent component to a child component via props. But passing props can become verbose and inconvenient if you have to pass them through many components in the middle, or if many components in your app need the same information. Context lets the parent component make some information available to any component in the tree below it—no matter how deep—without passing it explicitly through props.

리액트 공식문서에서는 Context를 props 깊게 전달해야될 때 함께 사용된다 라고 정의 내려져 있다. 컨텍스트는 부모 구성 요소가 그 아래의 전체 트리에 데이터를 제공하도록 한다. → 관리에 대한 내용이 전혀 없으며 값의 '전달', '공유'만을 언급한다.

나는 퍼넬 패턴을 고안할 때 Context API vs Recoil 이렇게 생각하며 → 즉 Context API == 상태 관리 라이브러리로 전역으로 상태를 관리하기 위해 Context API를 도입했는데, 애초에 이 둘은 비교선상이 알맞지 않았다.

📁 Props drilling이란,


https://react.dev/learn/passing-data-deeply-with-context

https://react.dev/learn/passing-data-deeply-with-context

But passing props can become verbose and inconvenient when you need to pass some prop deeply through the tree, or if many components need the same prop. The nearest common ancestor could be far removed from the components that need data, and lifting state up that high can lead to a situation called “prop drilling”.

스크린샷 2024-07-17 오후 3.18.40.png

이러한 Props drilling의 특징으로 Context를 이용하면, 트리 단계마다 명시적으로 props를 넘겨주지 않아도 많은 컴포넌트가 값을 공유하도록 할 수 있다.

<aside> <img src="/icons/subtitles_gray.svg" alt="/icons/subtitles_gray.svg" width="40px" /> 즉, Context API에 대하여 많은 개발자들이 useContext를 상태 관리를 위한 API로 오해하고 있다. 엄밀히 말하면 Context는 Props drilling 극복을 위한 방법 즉, 상태를 주입해 주는 API이다.

</aside>

2️⃣ Context 사용법


⓵  React.createContext 메소드를 통하여 context 객체를 만들어 사용한다.