mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-22 03:42:14 +00:00
removing Provider as in React 19 Context can be used as a provider (#7654)
This commit is contained in:
@@ -468,15 +468,15 @@ import { LevelContext } from './LevelContext.js';
|
||||
export default function Section({ level, children }) {
|
||||
return (
|
||||
<section className="section">
|
||||
<LevelContext.Provider value={level}>
|
||||
<LevelContext value={level}>
|
||||
{children}
|
||||
</LevelContext.Provider>
|
||||
</LevelContext>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
<Sandpack>
|
||||
|
||||
@@ -514,9 +514,9 @@ import { LevelContext } from './LevelContext.js';
|
||||
export default function Section({ level, children }) {
|
||||
return (
|
||||
<section className="section">
|
||||
<LevelContext.Provider value={level}>
|
||||
<LevelContext value={level}>
|
||||
{children}
|
||||
</LevelContext.Provider>
|
||||
</LevelContext>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -567,7 +567,7 @@ export const LevelContext = createContext(1);
|
||||
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:
|
||||
|
||||
1. You pass a `level` prop to the `<Section>`.
|
||||
2. `Section` wraps its children into `<LevelContext.Provider value={level}>`.
|
||||
2. `Section` wraps its children into `<LevelContext value={level}>`.
|
||||
3. `Heading` asks the closest value of `LevelContext` above with `useContext(LevelContext)`.
|
||||
|
||||
## 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 }) {
|
||||
const level = useContext(LevelContext);
|
||||
return (
|
||||
<section className="section">
|
||||
<LevelContext.Provider value={level + 1}>
|
||||
<LevelContext value={level + 1}>
|
||||
{children}
|
||||
</LevelContext.Provider>
|
||||
</LevelContext>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -643,9 +643,9 @@ export default function Section({ children }) {
|
||||
const level = useContext(LevelContext);
|
||||
return (
|
||||
<section className="section">
|
||||
<LevelContext.Provider value={level + 1}>
|
||||
<LevelContext value={level + 1}>
|
||||
{children}
|
||||
</LevelContext.Provider>
|
||||
</LevelContext>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -776,9 +776,9 @@ export default function Section({ children, isFancy }) {
|
||||
'section ' +
|
||||
(isFancy ? 'fancy' : '')
|
||||
}>
|
||||
<LevelContext.Provider value={level + 1}>
|
||||
<LevelContext value={level + 1}>
|
||||
{children}
|
||||
</LevelContext.Provider>
|
||||
</LevelContext>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -868,7 +868,7 @@ In general, if some information is needed by distant components in different par
|
||||
* To pass context:
|
||||
1. Create and export it with `export const MyContext = createContext(defaultValue)`.
|
||||
2. Pass it to the `useContext(MyContext)` Hook to read it in any child component, no matter how deep.
|
||||
3. Wrap children into `<MyContext.Provider value={...}>` to provide it from a parent.
|
||||
3. Wrap children into `<MyContext value={...}>` to provide it from a parent.
|
||||
* Context passes through any components in the middle.
|
||||
* Context lets you write components that "adapt to their surroundings".
|
||||
* Before you use context, try passing props or passing JSX as `children`.
|
||||
@@ -1022,7 +1022,7 @@ li {
|
||||
|
||||
Remove `imageSize` prop from all the components.
|
||||
|
||||
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`:
|
||||
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`:
|
||||
|
||||
<Sandpack>
|
||||
|
||||
@@ -1036,7 +1036,7 @@ export default function App() {
|
||||
const [isLarge, setIsLarge] = useState(false);
|
||||
const imageSize = isLarge ? 150 : 100;
|
||||
return (
|
||||
<ImageSizeContext.Provider
|
||||
<ImageSizeContext
|
||||
value={imageSize}
|
||||
>
|
||||
<label>
|
||||
@@ -1051,7 +1051,7 @@ export default function App() {
|
||||
</label>
|
||||
<hr />
|
||||
<List />
|
||||
</ImageSizeContext.Provider>
|
||||
</ImageSizeContext>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user