File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed
docs/basic/getting-started Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -1486,11 +1486,19 @@ export const App = () => (
1486
1486
<AppCtx.Provider value = { sampleAppContext } >...</AppCtx.Provider >
1487
1487
);
1488
1488
1489
+ const useAppContext = () => {
1490
+ const appContext = useContext (AppCtx );
1491
+ if (! appContext ) {
1492
+ throw new Error (" useApp must be used within <AppCtx.Provider>." );
1493
+ }
1494
+ return appContext ;
1495
+ };
1496
+
1489
1497
// Consume in your app
1490
1498
import { useContext } from " react" ;
1491
1499
1492
1500
export const PostInfo = () => {
1493
- const appContext = useContext ( AppCtx );
1501
+ const appContext = useAppContext ( );
1494
1502
return (
1495
1503
<div >
1496
1504
Name: { appContext .name } , Author: { appContext .author } , Url:{ " " }
Original file line number Diff line number Diff line change @@ -28,11 +28,19 @@ export const App = () => (
28
28
<AppCtx.Provider value = { sampleAppContext } >...</AppCtx.Provider >
29
29
);
30
30
31
+ const useAppContext = () => {
32
+ const appContext = useContext (AppCtx );
33
+ if (! appContext ) {
34
+ throw new Error (" useApp must be used within <AppCtx.Provider>." );
35
+ }
36
+ return appContext ;
37
+ };
38
+
31
39
// Consume in your app
32
40
import { useContext } from " react" ;
33
41
34
42
export const PostInfo = () => {
35
- const appContext = useContext ( AppCtx );
43
+ const appContext = useAppContext ( );
36
44
return (
37
45
<div >
38
46
Name: { appContext .name } , Author: { appContext .author } , Url:{ " " }
You can’t perform that action at this time.
0 commit comments