You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/passing-data-deeply-with-context.md
+16-16
Original file line number
Diff line number
Diff line change
@@ -468,15 +468,15 @@ import { LevelContext } from './LevelContext.js';
468
468
exportdefaultfunctionSection({ level, children }) {
469
469
return (
470
470
<section className="section">
471
-
<LevelContext.Provider value={level}>
471
+
<LevelContext value={level}>
472
472
{children}
473
-
</LevelContext.Provider>
473
+
</LevelContext>
474
474
</section>
475
475
);
476
476
}
477
477
```
478
478
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.
480
480
481
481
<Sandpack>
482
482
@@ -514,9 +514,9 @@ import { LevelContext } from './LevelContext.js';
514
514
exportdefaultfunctionSection({ level, children }) {
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:
568
568
569
569
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}>`.
571
571
3.`Heading` asks the closest value of `LevelContext` above with `useContext(LevelContext)`.
572
572
573
573
## 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 }) {
595
595
constlevel=useContext(LevelContext);
596
596
return (
597
597
<section className="section">
598
-
<LevelContext.Provider value={level +1}>
598
+
<LevelContext value={level +1}>
599
599
{children}
600
-
</LevelContext.Provider>
600
+
</LevelContext>
601
601
</section>
602
602
);
603
603
}
@@ -643,9 +643,9 @@ export default function Section({ children }) {
@@ -868,7 +868,7 @@ In general, if some information is needed by distant components in different par
868
868
* To pass context:
869
869
1. Create and export it with `exportconstMyContext=createContext(defaultValue)`.
870
870
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.
872
872
* Context passes through any components in the middle.
873
873
* Context lets you write components that "adapt to their surroundings".
874
874
* Before you use context, try passing props or passing JSX as `children`.
@@ -1022,7 +1022,7 @@ li {
1022
1022
1023
1023
Remove `imageSize` prop from all the components.
1024
1024
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`:
1026
1026
1027
1027
<Sandpack>
1028
1028
@@ -1036,7 +1036,7 @@ export default function App() {
1036
1036
const [isLarge, setIsLarge] =useState(false);
1037
1037
constimageSize= isLarge ?150:100;
1038
1038
return (
1039
-
<ImageSizeContext.Provider
1039
+
<ImageSizeContext
1040
1040
value={imageSize}
1041
1041
>
1042
1042
<label>
@@ -1051,7 +1051,7 @@ export default function App() {
0 commit comments