我的app.js
文件中有 2 个上下文:
function App() {
return (
<AuthContext>
<ChildContext>
<Template />
</ChildContext>
</AuthContext>
);
}
当用户注销时,我想访问里面的值ChildContext
,但logout
函数在里面AuthContext
:
// inside AuthContext:
const myContext = useContext(ChildContext);
const logout = () => {
// doing some stuff..
//
// here is the problem:
console.log(myContext.some_value);
}
错误是:
cannot read property 'some_value' of undefined
那是因为ChildContext
在AuthContext
. 那么我怎样才能进入some_value
里面AuthContext
呢?