Skip to content

Commit aa77d26

Browse files
committed
Replace Context.Provider with Context
Update to be in line with the recommended way from React 19. Docs https://react.dev/blog/2024/12/05/react-19#context-as-a-provider
1 parent e901790 commit aa77d26

File tree

14 files changed

+127
-130
lines changed

14 files changed

+127
-130
lines changed

src/components/Layout/HomeContent.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ async function Talks({ confId }) {
11721172
</CodeBlock>
11731173
}
11741174
right={
1175-
<NavContext.Provider value={{slug, navigate}}>
1175+
<NavContext value={{slug, navigate}}>
11761176
<BrowserChrome
11771177
domain="example.com"
11781178
path={'confs/' + slug}
@@ -1192,7 +1192,7 @@ async function Talks({ confId }) {
11921192
</Suspense>
11931193
</ExamplePanel>
11941194
</BrowserChrome>
1195-
</NavContext.Provider>
1195+
</NavContext>
11961196
}
11971197
/>
11981198
);

src/components/Layout/Page.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ export function Page({
8282
'max-w-7xl mx-auto',
8383
section === 'blog' && 'lg:flex lg:flex-col lg:items-center'
8484
)}>
85-
<TocContext.Provider value={toc}>
86-
<LanguagesContext.Provider value={languages}>
87-
{children}
88-
</LanguagesContext.Provider>
89-
</TocContext.Provider>
85+
<TocContext value={toc}>
86+
<LanguagesContext value={languages}>{children}</LanguagesContext>
87+
</TocContext>
9088
</div>
9189
{!isBlogIndex && (
9290
<DocsPageFooter

src/components/MDX/MDXComponents.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function IllustrationBlock({
354354
</figure>
355355
));
356356
return (
357-
<IllustrationContext.Provider value={isInBlockTrue}>
357+
<IllustrationContext value={isInBlockTrue}>
358358
<div className="relative group before:absolute before:-inset-y-16 before:inset-x-0 my-16 mx-0 2xl:mx-auto max-w-4xl 2xl:max-w-6xl">
359359
{sequential ? (
360360
<ol className="mdx-illustration-block flex">
@@ -369,7 +369,7 @@ function IllustrationBlock({
369369
)}
370370
<AuthorCredit author={author} authorLink={authorLink} />
371371
</div>
372-
</IllustrationContext.Provider>
372+
</IllustrationContext>
373373
);
374374
}
375375

src/content/learn/managing-state.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,9 @@ export default function Section({ children }) {
741741
const level = useContext(LevelContext);
742742
return (
743743
<section className="section">
744-
<LevelContext.Provider value={level + 1}>
744+
<LevelContext value={level + 1}>
745745
{children}
746-
</LevelContext.Provider>
746+
</LevelContext>
747747
</section>
748748
);
749749
}
@@ -836,13 +836,11 @@ export function TasksProvider({ children }) {
836836
);
837837

838838
return (
839-
<TasksContext.Provider value={tasks}>
840-
<TasksDispatchContext.Provider
841-
value={dispatch}
842-
>
839+
<TasksContext value={tasks}>
840+
<TasksDispatchContext value={dispatch}>
843841
{children}
844-
</TasksDispatchContext.Provider>
845-
</TasksContext.Provider>
842+
</TasksDispatchContext>
843+
</TasksContext>
846844
);
847845
}
848846

src/content/learn/scaling-up-with-reducer-and-context.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ export default function TaskApp() {
461461
const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
462462
// ...
463463
return (
464-
<TasksContext.Provider value={tasks}>
465-
<TasksDispatchContext.Provider value={dispatch}>
464+
<TasksContext value={tasks}>
465+
<TasksDispatchContext value={dispatch}>
466466
...
467-
</TasksDispatchContext.Provider>
468-
</TasksContext.Provider>
467+
</TasksDispatchContext>
468+
</TasksContext>
469469
);
470470
}
471471
```
@@ -509,8 +509,8 @@ export default function TaskApp() {
509509
}
510510

511511
return (
512-
<TasksContext.Provider value={tasks}>
513-
<TasksDispatchContext.Provider value={dispatch}>
512+
<TasksContext value={tasks}>
513+
<TasksDispatchContext value={dispatch}>
514514
<h1>Day off in Kyoto</h1>
515515
<AddTask
516516
onAddTask={handleAddTask}
@@ -520,8 +520,8 @@ export default function TaskApp() {
520520
onChangeTask={handleChangeTask}
521521
onDeleteTask={handleDeleteTask}
522522
/>
523-
</TasksDispatchContext.Provider>
524-
</TasksContext.Provider>
523+
</TasksDispatchContext>
524+
</TasksContext>
525525
);
526526
}
527527

@@ -676,13 +676,13 @@ In the next step, you will remove prop passing.
676676
Now you don't need to pass the list of tasks or the event handlers down the tree:
677677

678678
```js {4-5}
679-
<TasksContext.Provider value={tasks}>
680-
<TasksDispatchContext.Provider value={dispatch}>
679+
<TasksContext value={tasks}>
680+
<TasksDispatchContext value={dispatch}>
681681
<h1>Day off in Kyoto</h1>
682682
<AddTask />
683683
<TaskList />
684-
</TasksDispatchContext.Provider>
685-
</TasksContext.Provider>
684+
</TasksDispatchContext>
685+
</TasksContext>
686686
```
687687

688688
Instead, any component that needs the task list can read it from the `TaskContext`:
@@ -730,13 +730,13 @@ export default function TaskApp() {
730730
);
731731

732732
return (
733-
<TasksContext.Provider value={tasks}>
734-
<TasksDispatchContext.Provider value={dispatch}>
733+
<TasksContext value={tasks}>
734+
<TasksDispatchContext value={dispatch}>
735735
<h1>Day off in Kyoto</h1>
736736
<AddTask />
737737
<TaskList />
738-
</TasksDispatchContext.Provider>
739-
</TasksContext.Provider>
738+
</TasksDispatchContext>
739+
</TasksContext>
740740
);
741741
}
742742

@@ -921,11 +921,11 @@ export function TasksProvider({ children }) {
921921
const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
922922

923923
return (
924-
<TasksContext.Provider value={tasks}>
925-
<TasksDispatchContext.Provider value={dispatch}>
924+
<TasksContext value={tasks}>
925+
<TasksDispatchContext value={dispatch}>
926926
{children}
927-
</TasksDispatchContext.Provider>
928-
</TasksContext.Provider>
927+
</TasksDispatchContext>
928+
</TasksContext>
929929
);
930930
}
931931
```
@@ -963,11 +963,11 @@ export function TasksProvider({ children }) {
963963
);
964964

965965
return (
966-
<TasksContext.Provider value={tasks}>
967-
<TasksDispatchContext.Provider value={dispatch}>
966+
<TasksContext value={tasks}>
967+
<TasksDispatchContext value={dispatch}>
968968
{children}
969-
</TasksDispatchContext.Provider>
970-
</TasksContext.Provider>
969+
</TasksDispatchContext>
970+
</TasksContext>
971971
);
972972
}
973973

@@ -1174,11 +1174,11 @@ export function TasksProvider({ children }) {
11741174
);
11751175

11761176
return (
1177-
<TasksContext.Provider value={tasks}>
1178-
<TasksDispatchContext.Provider value={dispatch}>
1177+
<TasksContext value={tasks}>
1178+
<TasksDispatchContext value={dispatch}>
11791179
{children}
1180-
</TasksDispatchContext.Provider>
1181-
</TasksContext.Provider>
1180+
</TasksDispatchContext>
1181+
</TasksContext>
11821182
);
11831183
}
11841184

@@ -1363,4 +1363,3 @@ As your app grows, you may have many context-reducer pairs like this. This is a
13631363
- You can have many context-reducer pairs like this in your app.
13641364
13651365
</Recap>
1366-

src/content/learn/typescript.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ export default function MyApp() {
260260
const [theme, setTheme] = useState<Theme>('light');
261261

262262
return (
263-
<ThemeContext.Provider value={theme}>
263+
<ThemeContext value={theme}>
264264
<MyComponent />
265-
</ThemeContext.Provider>
265+
</ThemeContext>
266266
)
267267
}
268268

@@ -310,9 +310,9 @@ export default function MyApp() {
310310
const object = useMemo(() => ({ kind: "complex" }), []);
311311

312312
return (
313-
<Context.Provider value={object}>
313+
<Context value={object}>
314314
<MyComponent />
315-
</Context.Provider>
315+
</Context>
316316
)
317317
}
318318

src/content/reference/react/Component.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,9 +1814,9 @@ function Form() {
18141814

18151815
export default function MyApp() {
18161816
return (
1817-
<ThemeContext.Provider value="dark">
1817+
<ThemeContext value="dark">
18181818
<Form />
1819-
</ThemeContext.Provider>
1819+
</ThemeContext>
18201820
)
18211821
}
18221822
```
@@ -1900,9 +1900,9 @@ function Form() {
19001900

19011901
export default function MyApp() {
19021902
return (
1903-
<ThemeContext.Provider value="dark">
1903+
<ThemeContext value="dark">
19041904
<Form />
1905-
</ThemeContext.Provider>
1905+
</ThemeContext>
19061906
)
19071907
}
19081908
```

src/content/reference/react/cloneElement.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ export default function List({ items, renderItem }) {
414414
{items.map((item, index) => {
415415
const isHighlighted = index === selectedIndex;
416416
return (
417-
<HighlightContext.Provider key={item.id} value={isHighlighted}>
417+
<HighlightContext key={item.id} value={isHighlighted}>
418418
{renderItem(item)}
419-
</HighlightContext.Provider>
419+
</HighlightContext>
420420
);
421421
})}
422422
```
@@ -472,12 +472,12 @@ export default function List({ items, renderItem }) {
472472
{items.map((item, index) => {
473473
const isHighlighted = index === selectedIndex;
474474
return (
475-
<HighlightContext.Provider
475+
<HighlightContext
476476
key={item.id}
477477
value={isHighlighted}
478478
>
479479
{renderItem(item)}
480-
</HighlightContext.Provider>
480+
</HighlightContext>
481481
);
482482
})}
483483
<hr />

src/content/reference/react/createContext.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ const ThemeContext = createContext('light');
3838

3939
`createContext` returns a context object.
4040

41-
**The context object itself does not hold any information.** It represents _which_ context other components read or provide. Typically, you will use [`SomeContext.Provider`](#provider) in components above to specify the context value, and call [`useContext(SomeContext)`](/reference/react/useContext) in components below to read it. The context object has a few properties:
41+
**The context object itself does not hold any information.** It represents _which_ context other components read or provide. Typically, you will use [`SomeContext`](#provider) in components above to specify the context value, and call [`useContext(SomeContext)`](/reference/react/useContext) in components below to read it. The context object has a few properties:
4242

43-
* `SomeContext.Provider` lets you provide the context value to components.
43+
* `SomeContext` lets you provide the context value to components.
44+
* `SomeContext.Provider` a legacy way to provide the context value.
4445
* `SomeContext.Consumer` is an alternative and rarely used way to read the context value.
4546

4647
---
4748

48-
### `SomeContext.Provider` {/*provider*/}
49+
### `SomeContext` or `SomeContext.Provider` {/*provider*/}
4950

5051
Wrap your components into a context provider to specify the value of this context for all components inside:
5152

@@ -54,13 +55,15 @@ function App() {
5455
const [theme, setTheme] = useState('light');
5556
// ...
5657
return (
57-
<ThemeContext.Provider value={theme}>
58+
<ThemeContext value={theme}>
5859
<Page />
59-
</ThemeContext.Provider>
60+
</ThemeContext>
6061
);
6162
}
6263
```
6364

65+
`SomeContext.Provider` is an alias for `SomeContext` and was used in older versions of React before 19.0.
66+
6467
#### Props {/*provider-props*/}
6568

6669
* `value`: The value that you want to pass to all the components reading this context inside this provider, no matter how deep. The context value can be of any type. A component calling [`useContext(SomeContext)`](/reference/react/useContext) inside of the provider receives the `value` of the innermost corresponding context provider above it.
@@ -141,11 +144,11 @@ function App() {
141144
// ...
142145

143146
return (
144-
<ThemeContext.Provider value={theme}>
145-
<AuthContext.Provider value={currentUser}>
147+
<ThemeContext value={theme}>
148+
<AuthContext value={currentUser}>
146149
<Page />
147-
</AuthContext.Provider>
148-
</ThemeContext.Provider>
150+
</AuthContext>
151+
</ThemeContext>
149152
);
150153
}
151154
```
@@ -187,11 +190,11 @@ import { ThemeContext, AuthContext } from './Contexts.js';
187190
function App() {
188191
// ...
189192
return (
190-
<ThemeContext.Provider value={theme}>
191-
<AuthContext.Provider value={currentUser}>
193+
<ThemeContext value={theme}>
194+
<AuthContext value={currentUser}>
192195
<Page />
193-
</AuthContext.Provider>
194-
</ThemeContext.Provider>
196+
</AuthContext>
197+
</ThemeContext>
195198
);
196199
}
197200
```
@@ -214,4 +217,3 @@ const ThemeContext = createContext('light');
214217
This value never changes. React only uses this value as a fallback if it can't find a matching provider above.
215218

216219
To make context change over time, [add state and wrap components in a context provider.](/reference/react/useContext#updating-data-passed-via-context)
217-

src/content/reference/react/legacy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ These APIs were removed in React 19:
3030
* [`createFactory`](https://18.react.dev/reference/react/createFactory): use JSX instead.
3131
* Class Components: [`static contextTypes`](https://18.react.dev//reference/react/Component#static-contexttypes): use [`static contextType`](#static-contexttype) instead.
3232
* Class Components: [`static childContextTypes`](https://18.react.dev//reference/react/Component#static-childcontexttypes): use [`static contextType`](#static-contexttype) instead.
33-
* Class Components: [`static getChildContext`](https://18.react.dev//reference/react/Component#getchildcontext): use [`Context.Provider`](/reference/react/createContext#provider) instead.
33+
* Class Components: [`static getChildContext`](https://18.react.dev//reference/react/Component#getchildcontext): use [`Context`](/reference/react/createContext#provider) instead.
3434
* Class Components: [`static propTypes`](https://18.react.dev//reference/react/Component#static-proptypes): use a type system like [TypeScript](https://www.typescriptlang.org/) instead.
3535
* Class Components: [`this.refs`](https://18.react.dev//reference/react/Component#refs): use [`createRef`](/reference/react/createRef) instead.

src/content/reference/react/memo.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ export default function MyApp() {
226226
}
227227

228228
return (
229-
<ThemeContext.Provider value={theme}>
229+
<ThemeContext value={theme}>
230230
<button onClick={handleClick}>
231231
Switch theme
232232
</button>
233233
<Greeting name="Taylor" />
234-
</ThemeContext.Provider>
234+
</ThemeContext>
235235
);
236236
}
237237

0 commit comments

Comments
 (0)