useContext 는 컴포넌트에서
context를 읽고 구독할 수 있는 React Hook입니다.
<Grandparent props={something}>
<Parant props={something}>
<Child props={something}>
<Grandchild props={something}>
</Grandchild>
</Child>
</Parant>
</Grandparent>

import { createContext } from 'react';
const MyContext = createContext();
function MyComponent() {
const { value, setValue } = useContext(MyContext);
return (
<div>
<p>{value}</p>
<button onClick={() => setValue("Bye")}>Change Value</button>
</div>
);
}
function MyProvider() {
const [value, setValue] = useState("Hello");
return (
<MyContext.Provider value={{ value, setValue }}>
<MyComponent />
</MyContext.Provider>
);
}
// App.js
import { useState } from 'react';
import { ThemeContext } from './ThemeContext';
import Section from './Section';
import { SunIcon, MoonIcon } from '@heroicons/react/24/solid';
const App = () => {
const [theme, setTheme] = useState("light");
return (
<ThemeContext.Provider value={theme}>
<Section />
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
{
theme === 'light' ?
<MoonIcon className='w-10' /> : <SunIcon className='w-10' />
}
</button>
</ThemeContext.Provider>
)
}
export default App;
// theme.js
import { createContext } from 'react';
export const ThemeContext = createContext('light');
// section.jsx
import { useContext } from 'react';
import { ThemeContext } from './ThemeContext';
const Section = () => {
const theme = useContext(ThemeContext);
return (
<section className={`${theme === "light" ? 'bg-white text-black' : 'bg-black text-white' }`}>
<h1>Hello</h1>
</section>
)
};
export default Section;

참고
https://ko.react.dev/reference/react/useContext
참고