简体   繁体   中英

useContext returns undefined in child component

I'm learning hooks and trying to understand useContext . The component is a child of the provider ,

But when I see the value returned by useContext , it is undefined .

https://codesandbox.io/s/divine-star-2mz87?file=/src/C2.js

In the component C2, I get undefined

you should use named import in c2:

import {CContext} from "./App";

instead of default import which will give you App react component import CContext from "./App";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM