Skip to content

Commit c60173f

Browse files
docs: Refactor context provider usage (#7793)
* delete provider * Fix NavContext usage in Talks component * Fix TocContext and LanguagesContext usage in Page component * Fix IllustrationContext usage in IllustrationBlock component * Fix LevelContext and TasksContext usage in managing-state.md * Fix ThemeContext and Context usage in MyApp component * Fix HighlightContext usage in List component * Fix ThemeContext usage in MyApp component * Fix ErrorDecoderContext usage in ErrorDecoderPage component * Fix ThemeContext usage in MyPage and MyApp components * Fix ThemeContext usage in MyApp component * Fix useContext documentation to correct context provider references * Fix context provider references in createContext documentation * prettier * Update src/content/reference/react/createContext.md --------- Co-authored-by: Ricky <[email protected]>
1 parent 87cef4a commit c60173f

File tree

13 files changed

+124
-125
lines changed

13 files changed

+124
-125
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 & 6 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,13 @@ export function TasksProvider({ children }) {
836836
);
837837

838838
return (
839-
<TasksContext.Provider value={tasks}>
840-
<TasksDispatchContext.Provider
839+
<TasksContext value={tasks}>
840+
<TasksDispatchContext
841841
value={dispatch}
842842
>
843843
{children}
844-
</TasksDispatchContext.Provider>
845-
</TasksContext.Provider>
844+
</TasksDispatchContext>
845+
</TasksContext>
846846
);
847847
}
848848

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

Lines changed: 28 additions & 28 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

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: 14 additions & 13 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.
4444
* `SomeContext.Consumer` is an alternative and rarely used way to read the context value.
45+
* `SomeContext.Provider` is a legacy way to provide the context value before React 19.
4546

4647
---
4748

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

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

@@ -54,9 +55,9 @@ 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
```
@@ -141,11 +142,11 @@ function App() {
141142
// ...
142143

143144
return (
144-
<ThemeContext.Provider value={theme}>
145-
<AuthContext.Provider value={currentUser}>
145+
<ThemeContext value={theme}>
146+
<AuthContext value={currentUser}>
146147
<Page />
147-
</AuthContext.Provider>
148-
</ThemeContext.Provider>
148+
</AuthContext>
149+
</ThemeContext>
149150
);
150151
}
151152
```
@@ -187,11 +188,11 @@ import { ThemeContext, AuthContext } from './Contexts.js';
187188
function App() {
188189
// ...
189190
return (
190-
<ThemeContext.Provider value={theme}>
191-
<AuthContext.Provider value={currentUser}>
191+
<ThemeContext value={theme}>
192+
<AuthContext value={currentUser}>
192193
<Page />
193-
</AuthContext.Provider>
194-
</ThemeContext.Provider>
194+
</AuthContext>
195+
</ThemeContext>
195196
);
196197
}
197198
```

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

src/content/reference/react/use.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ To pass context to a `Button`, wrap it or one of its parent components into the
7474
```js [[1, 3, "ThemeContext"], [2, 3, "\\"dark\\""], [1, 5, "ThemeContext"]]
7575
function MyPage() {
7676
return (
77-
<ThemeContext.Provider value="dark">
77+
<ThemeContext value="dark">
7878
<Form />
79-
</ThemeContext.Provider>
79+
</ThemeContext>
8080
);
8181
}
8282

@@ -116,9 +116,9 @@ const ThemeContext = createContext(null);
116116

117117
export default function MyApp() {
118118
return (
119-
<ThemeContext.Provider value="dark">
119+
<ThemeContext value="dark">
120120
<Form />
121-
</ThemeContext.Provider>
121+
</ThemeContext>
122122
)
123123
}
124124

0 commit comments

Comments
 (0)