--- title: createContext --- `createContext`를 사용하면 컴포넌트가 [Context](/learn/passing-data-deeply-with-context)를 제공하거나 읽을 수 있습니다. ```js const SomeContext = createContext(defaultValue) ``` --- ## 레퍼런스 {/*reference*/} ### `createContext(defaultValue)` {/*createcontext*/} 컴포넌트 외부에서 `createContext`를 호출하여 컨텍스트를 생성합니다. ```js import { createContext } from 'react'; const ThemeContext = createContext('light'); ``` [아래 예시를 참고하세요.](#usage) #### 매개변수 {/*parameters*/} * `defaultValue`: 컴포넌트가 컨텍스트를 읽을 때 상위에 일치하는 컨텍스트 제공자가 없는 경우 컨텍스트가 가져야 할 값입니다. 의미 있는 기본값이 없으면 `null`을 지정하세요. 기본값은 "최후의 수단"으로 사용됩니다. 이 값은 정적이며 시간이 지나도 변경되지 않습니다. #### 반환값 {/*returns*/} `createContext` returns a context object. **컨텍스트 객체 자체는 어떠한 정보도 가지고 있지 않습니다.** 다른 컴포넌트가 읽거나 제공하는 어떤 컨텍스트를 나타냅니다. 일반적으로 상위 컴포넌트에서 컨텍스트 값을 지정하기 위해 [`SomeContext`](#provider)를 사용하고, 아래 컴포넌트에서 읽기 위해 [`useContext(SomeContext)`](/reference/react/useContext)를 호출합니다. 컨텍스트 객체에는 몇 가지 속성이 있습니다. * `SomeContext` lets you provide the context value to components. * `SomeContext.Consumer`는 컨텍스트 값을 읽는 대안이며 드물게 사용됩니다. * `SomeContext.Provider` is a legacy way to provide the context value before React 19. --- ### `SomeContext` Provider {/*provider*/} 컴포넌트를 컨텍스트 제공자로 감싸서 이 컨텍스트의 값을 모든 내부 컴포넌트에 지정합니다. ```js function App() { const [theme, setTheme] = useState('light'); // ... return ( ); } ``` Starting in React 19, you can render `` as a provider. In older versions of React, use ``. #### Props {/*provider-props*/} * `value`: 이 제공자 내부의 컨텍스트를 읽는 모든 컴포넌트에 전달하려는 값입니다. 컨텍스트 값은 어떠한 유형이든 될 수 있습니다. 제공자 내부에서 [`useContext(SomeContext)`](/reference/react/useContext)를 호출하는 컴포넌트는 그 위의 가장 가까운 해당 컨텍스트 제공자의 `value`를 받게 됩니다. --- ### `SomeContext.Consumer` {/*consumer*/} `useContext`가 등장하기 전에 컨텍스트를 읽는 이전 방식이 있었습니다. ```js function Button() { // 🟡 이전 방식 (권장하지 않음) return ( {theme => (