Skip to content

Commit cc774fa

Browse files
removing Provider as in React 19 Context can be used as a provider (#7654)
1 parent 4a4e579 commit cc774fa

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/content/learn/passing-data-deeply-with-context.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,15 @@ import { LevelContext } from './LevelContext.js';
468468
export default function Section({ level, children }) {
469469
return (
470470
<section className="section">
471-
<LevelContext.Provider value={level}>
471+
<LevelContext value={level}>
472472
{children}
473-
</LevelContext.Provider>
473+
</LevelContext>
474474
</section>
475475
);
476476
}
477477
```
478478

479-
This tells React: "if any component inside this `<Section>` asks for `LevelContext`, give them this `level`." The component will use the value of the nearest `<LevelContext.Provider>` in the UI tree above it.
479+
This tells React: "if any component inside this `<Section>` asks for `LevelContext`, give them this `level`." The component will use the value of the nearest `<LevelContext>` in the UI tree above it.
480480

481481
<Sandpack>
482482

@@ -514,9 +514,9 @@ import { LevelContext } from './LevelContext.js';
514514
export default function Section({ level, children }) {
515515
return (
516516
<section className="section">
517-
<LevelContext.Provider value={level}>
517+
<LevelContext value={level}>
518518
{children}
519-
</LevelContext.Provider>
519+
</LevelContext>
520520
</section>
521521
);
522522
}
@@ -567,7 +567,7 @@ export const LevelContext = createContext(1);
567567
It's the same result as the original code, but you did not need to pass the `level` prop to each `Heading` component! Instead, it "figures out" its heading level by asking the closest `Section` above:
568568

569569
1. You pass a `level` prop to the `<Section>`.
570-
2. `Section` wraps its children into `<LevelContext.Provider value={level}>`.
570+
2. `Section` wraps its children into `<LevelContext value={level}>`.
571571
3. `Heading` asks the closest value of `LevelContext` above with `useContext(LevelContext)`.
572572

573573
## Using and providing context from the same component {/*using-and-providing-context-from-the-same-component*/}
@@ -595,9 +595,9 @@ export default function Section({ children }) {
595595
const level = useContext(LevelContext);
596596
return (
597597
<section className="section">
598-
<LevelContext.Provider value={level + 1}>
598+
<LevelContext value={level + 1}>
599599
{children}
600-
</LevelContext.Provider>
600+
</LevelContext>
601601
</section>
602602
);
603603
}
@@ -643,9 +643,9 @@ export default function Section({ children }) {
643643
const level = useContext(LevelContext);
644644
return (
645645
<section className="section">
646-
<LevelContext.Provider value={level + 1}>
646+
<LevelContext value={level + 1}>
647647
{children}
648-
</LevelContext.Provider>
648+
</LevelContext>
649649
</section>
650650
);
651651
}
@@ -776,9 +776,9 @@ export default function Section({ children, isFancy }) {
776776
'section ' +
777777
(isFancy ? 'fancy' : '')
778778
}>
779-
<LevelContext.Provider value={level + 1}>
779+
<LevelContext value={level + 1}>
780780
{children}
781-
</LevelContext.Provider>
781+
</LevelContext>
782782
</section>
783783
);
784784
}
@@ -868,7 +868,7 @@ In general, if some information is needed by distant components in different par
868868
* To pass context:
869869
1. Create and export it with `export const MyContext = createContext(defaultValue)`.
870870
2. Pass it to the `useContext(MyContext)` Hook to read it in any child component, no matter how deep.
871-
3. Wrap children into `<MyContext.Provider value={...}>` to provide it from a parent.
871+
3. Wrap children into `<MyContext value={...}>` to provide it from a parent.
872872
* Context passes through any components in the middle.
873873
* Context lets you write components that "adapt to their surroundings".
874874
* Before you use context, try passing props or passing JSX as `children`.
@@ -1022,7 +1022,7 @@ li {
10221022
10231023
Remove `imageSize` prop from all the components.
10241024
1025-
Create and export `ImageSizeContext` from `Context.js`. Then wrap the List into `<ImageSizeContext.Provider value={imageSize}>` to pass the value down, and `useContext(ImageSizeContext)` to read it in the `PlaceImage`:
1025+
Create and export `ImageSizeContext` from `Context.js`. Then wrap the List into `<ImageSizeContext value={imageSize}>` to pass the value down, and `useContext(ImageSizeContext)` to read it in the `PlaceImage`:
10261026
10271027
<Sandpack>
10281028
@@ -1036,7 +1036,7 @@ export default function App() {
10361036
const [isLarge, setIsLarge] = useState(false);
10371037
const imageSize = isLarge ? 150 : 100;
10381038
return (
1039-
<ImageSizeContext.Provider
1039+
<ImageSizeContext
10401040
value={imageSize}
10411041
>
10421042
<label>
@@ -1051,7 +1051,7 @@ export default function App() {
10511051
</label>
10521052
<hr />
10531053
<List />
1054-
</ImageSizeContext.Provider>
1054+
</ImageSizeContext>
10551055
)
10561056
}
10571057

0 commit comments

Comments
 (0)