Reactjs useContext返回未定义的上下文,但Context.Consumer工作正常

Reactjs useContext返回未定义的上下文,但Context.Consumer工作正常,reactjs,react-context,Reactjs,React Context,我正在尝试通过布局的子级传递一些数据。使用“Context.Consumer”,数据是没有任何问题的,但是使用“useContext”,我得到的只是“undefined” 您可以在此处检查整个代码: 我的上下文:companyContext.ts 从“react”导入{createContext}; 从“../../../services”导入{UserCompanyType}; 类型TypeCompanyData={ 公司:UserCompanyType |空; 加载:布尔; }; 导出默认c

我正在尝试通过布局的子级传递一些数据。使用“Context.Consumer”,数据是没有任何问题的,但是使用“useContext”,我得到的只是“undefined”

您可以在此处检查整个代码:

我的上下文:companyContext.ts

从“react”导入{createContext};
从“../../../services”导入{UserCompanyType};
类型TypeCompanyData={
公司:UserCompanyType |空;
加载:布尔;
};
导出默认createContext({}作为TypeCompanyData);
我的提供者:layout.tsx

const[loading,setLoading]=useState(true);
const[company,setCompany]=使用状态(companyData);
返回(
{儿童}
);
布局的子级:departments.tsx

const{loading,company}=useContext(Context);
useffect(()=>{
console.log(加载,公司);//返回未定义
},[公司,装货];
返回(
//很好
{context=>
context.company&&(
)
}
history.push('departmentos/cadastar')}
/>
);

我想我明白了。提供商正在调整包装子应用程序,而不是整个应用程序或部门组件。因此,我可以访问JSX内部的上下文,即布局的子级。这是正确的吗?