@@ -471,15 +471,15 @@ import { LevelContext } from './LevelContext.js';
471471export default function Section ({ level, children }) {
472472 return (
473473 < section className= " section" >
474- < LevelContext . Provider value= {level}>
474+ < LevelContext value= {level}>
475475 {children}
476- < / LevelContext . Provider >
476+ < / LevelContext>
477477 < / section>
478478 );
479479}
480480```
481481
482- 这告诉 React:“如果在 ` <Section> ` 组件中的任何子组件请求 ` LevelContext ` ,给他们这个 ` level ` 。”组件会使用 UI 树中在它上层最近的那个 ` <LevelContext.Provider > ` 传递过来的值。
482+ 这告诉 React:“如果在 ` <Section> ` 组件中的任何子组件请求 ` LevelContext ` ,给他们这个 ` level ` 。”组件会使用 UI 树中在它上层最近的那个 ` <LevelContext> ` 传递过来的值。
483483
484484<Sandpack >
485485
@@ -517,9 +517,9 @@ import { LevelContext } from './LevelContext.js';
517517export default function Section ({ level, children }) {
518518 return (
519519 < section className= " section" >
520- < LevelContext . Provider value= {level}>
520+ < LevelContext value= {level}>
521521 {children}
522- < / LevelContext . Provider >
522+ < / LevelContext>
523523 < / section>
524524 );
525525}
@@ -570,7 +570,7 @@ export const LevelContext = createContext(1);
570570这与原始代码的运行结果相同,但是你不需要向每个 ` Heading ` 组件传递 ` level ` 参数了!取而代之的是,它通过访问上层最近的 ` Section ` 来“断定”它的标题级别:
571571
5725721 . 你将一个 ` level ` 参数传递给 ` <Section> ` 。
573- 2 . ` Section ` 把它的子元素包在 ` <LevelContext.Provider value={level}> ` 里面。
573+ 2 . ` Section ` 把它的子元素包在 ` <LevelContext value={level}> ` 里面。
5745743 . ` Heading ` 使用 ` useContext(LevelContext) ` 访问上层最近的 ` LevelContext ` 提供的值。
575575
576576## 在相同的组件中使用并提供 context {/* using-and-providing-context-from-the-same-component* /}
@@ -599,9 +599,9 @@ export default function Section({ children }) {
599599 const level = useContext (LevelContext);
600600 return (
601601 < section className= " section" >
602- < LevelContext . Provider value= {level + 1 }>
602+ < LevelContext value= {level + 1 }>
603603 {children}
604- < / LevelContext . Provider >
604+ < / LevelContext>
605605 < / section>
606606 );
607607}
@@ -647,9 +647,9 @@ export default function Section({ children }) {
647647 const level = useContext (LevelContext);
648648 return (
649649 < section className= " section" >
650- < LevelContext . Provider value= {level + 1 }>
650+ < LevelContext value= {level + 1 }>
651651 {children}
652- < / LevelContext . Provider >
652+ < / LevelContext>
653653 < / section>
654654 );
655655}
@@ -780,9 +780,9 @@ export default function Section({ children, isFancy }) {
780780 ' section ' +
781781 (isFancy ? ' fancy' : ' ' )
782782 }>
783- < LevelContext . Provider value= {level + 1 }>
783+ < LevelContext value= {level + 1 }>
784784 {children}
785- < / LevelContext . Provider >
785+ < / LevelContext>
786786 < / section>
787787 );
788788}
@@ -872,7 +872,7 @@ Context 不局限于静态值。如果你在下一次渲染时传递不同的值
872872* 传递 Context 的方法:
873873 1. 通过 ` export const MyContext = createContext (defaultValue)` 创建并导出 context。
874874 2. 在无论层级多深的任何子组件中,把 context 传递给 ` useContext (MyContext)` Hook 来读取它。
875- 3. 在父组件中把 children 包在 ` < MyContext . Provider value= {... }> ` 中来提供 context。
875+ 3. 在父组件中把 children 包在 ` < MyContext value= {... }> ` 中来提供 context。
876876* Context 会穿过中间的任何组件。
877877* Context 可以让你写出 “较为通用” 的组件。
878878* 在使用 context 之前,先试试传递 props 或者将 JSX 作为 ` children` 传递。
@@ -1026,7 +1026,7 @@ li {
10261026
10271027移除掉所有组件中的 ` imageSize` 参数。
10281028
1029- 在 ` Context .js ` 中创建并导出 ` ImageSizeContext` 。然后用 ` < ImageSizeContext . Provider value= {imageSize}> ` 包裹住整个列表来向下传递值,最后在 ` PlaceImage` 中使用 ` useContext (ImageSizeContext)` 来读取它。
1029+ 在 ` Context .js ` 中创建并导出 ` ImageSizeContext` 。然后用 ` < ImageSizeContext value= {imageSize}> ` 包裹住整个列表来向下传递值,最后在 ` PlaceImage` 中使用 ` useContext (ImageSizeContext)` 来读取它。
10301030
10311031<Sandpack>
10321032
@@ -1040,7 +1040,7 @@ export default function App() {
10401040 const [isLarge , setIsLarge ] = useState (false );
10411041 const imageSize = isLarge ? 150 : 100 ;
10421042 return (
1043- < ImageSizeContext . Provider
1043+ < ImageSizeContext
10441044 value= {imageSize}
10451045 >
10461046 < label>
@@ -1055,7 +1055,7 @@ export default function App() {
10551055 < / label>
10561056 < hr / >
10571057 < List / >
1058- < / ImageSizeContext . Provider >
1058+ < / ImageSizeContext>
10591059 )
10601060}
10611061
0 commit comments