File tree 2 files changed +18
-2
lines changed
docs/basic/getting-started 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -1529,11 +1529,19 @@ export const App = () => (
1529
1529
<AppCtx.Provider value = { sampleAppContext } >...</AppCtx.Provider >
1530
1530
);
1531
1531
1532
+ const useAppContext = () => {
1533
+ const appContext = useContext (AppCtx );
1534
+ if (! appContext ) {
1535
+ throw new Error (" useApp must be used within <AppCtx.Provider>." );
1536
+ }
1537
+ return appContext ;
1538
+ };
1539
+
1532
1540
// Consume in your app
1533
1541
import { useContext } from " react" ;
1534
1542
1535
1543
export const PostInfo = () => {
1536
- const appContext = useContext ( AppCtx );
1544
+ const appContext = useAppContext ( );
1537
1545
return (
1538
1546
<div >
1539
1547
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