File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed
Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -1173,4 +1173,31 @@ const memoizedCallback = useCallback(
11731173 doSomething ( a , b ) ;
11741174 } ,
11751175 [ a , b ] ,
1176- ) ;
1176+ ) ;
1177+
1178+
1179+
1180+ // CONTEXT
1181+ // very useful to pass data deep in the tree
1182+ import { createContext , useState } from 'react' ;
1183+ export const CountContext = createContext ( ) ;
1184+ const FunctionalComponent = ( ) => {
1185+ const [ count , setCount ] = useState ( 0 ) ;
1186+ return (
1187+ < CountContext . Provider value = { setCount , count } >
1188+ < ChildComponent />
1189+ </ CountContext . Provider >
1190+ )
1191+ }
1192+
1193+ import { useContext } from 'react' ;
1194+ import { CountContext } from './FunctionalComponent' ;
1195+ const ChildComponent = ( ) => {
1196+ const { setCount, count} = useContext ( CountContext ) ;
1197+ return (
1198+ < div >
1199+ < p > count: { count } </ p >
1200+ < button onClick = { ( ) => setCount ( count + 1 ) } > Click</ button >
1201+ </ div >
1202+ )
1203+ }
You can’t perform that action at this time.
0 commit comments