createContext Creating Context

Last updated: 2026-11-02

createContext Creating Context

createContext creates a Context object that lets data travel across levels of the tree without threading props through every intermediate component. It ships with a Provider component that broadcasts a value downwards and pairs with useContext on the reading side. It suits genuinely global data such as a theme, a language or the current user. The object itself is the key: useContext locates the matching Provider by identity, so the same context can be created once at module level and consumed from anywhere.

GroupComponent Patterns
KindAPI
SinceReact 16.3

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

import { createContext } from 'react'

// the default value applies when there is no Provider upstream
const ThemeContext = createContext('light')

<ThemeContext.Provider value="dark">
  …every component downstream can read 'dark'…
</ThemeContext.Provider>

⚙️ Parameters

defaultValueThe fallback value returned by useContext when no matching Provider is found downstream; passing undefined explicitly makes a Provider mandatory.

Returns:Returns a Context object: it carries the Provider and Consumer components and is itself the key that useContext reads.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

The page shows "Hello!" — App uses the Provider to broadcast en to a deep component, and Greeting reads it without receiving a single prop.

💡 Key Points

💡 Tip The argument of createContext is a fallback default rather than an initial value: as soon as a Provider exists upstream, the value of that Provider wins.
ℹ️ Info A changed context value re-renders every consumer, which is why the useState setter is often bundled into the value of the Provider.
⚠️ Warning Do not turn every prop into context — use it only for data that is truly global, or the reuse and readability of your components will suffer.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Is the argument of createContext an initial value?
A It only acts as a fallback when there is no Provider upstream; when a Provider exists, its value overrides the default completely.
Q Must the Provider sit above the consuming component?
A Yes. useContext walks up the component tree to the nearest Provider and only falls back to the default value when it finds none.
Q How do I choose between Context and props?
A Within two levels of nesting, pass props; reach for Context only when the data has to cross many levels and is shared globally, such as a theme, a user or a language.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏