简体   繁体   English

通过 useContext 使上下文对子组件可用

[英]Making Context Available to Child Component via useContext

I'm moving a React app from class to functional components where it makes sense to, however I'm having a little trouble with the useContext hook, or perhaps just the context API in general.我正在将 React 应用程序从 class 移动到有意义的功能组件中,但是我在useContext钩子上遇到了一些问题,或者可能只是上下文 API 。

My application supports authenticated and non-authenticated states.我的应用程序支持经过身份验证和未经过身份验证的状态。 For authenticated users I would like to make an API call which returns details of items that they own and present this in a table.对于经过身份验证的用户,我想进行 API 调用,该调用返回他们拥有的项目的详细信息并将其显示在表格中。 Because this list is reasonably static I would like to store it in context and pass it down to child components which will show different views on that data.因为这个列表是合理的 static 我想将它存储在上下文中并将其传递给子组件,这些子组件将显示对该数据的不同视图。 I don't want to be going back to that API for different components.对于不同的组件,我不想回到那个 API。 Authenticated users and non authenticated users will also be able to see items that other people own by visiting their pages/routes eg user/one, user/two.经过身份验证的用户和未经身份验证的用户也将能够通过访问其他人的页面/路线来查看他们拥有的项目,例如用户/一、用户/二。 I think this data will be stored in the component state.我认为这些数据将存储在组件 state 中。

I have a stackblitz at https://stackblitz.com/edit/react-jvmryf which shows what I am doing.我在https://stackblitz.com/edit/react-jvmryf有一个 stackblitz,它显示了我在做什么。

I have created a context ( UserContext ) which is shared to child components and ideally I would like the value of UserContext.data to provide the value of the table, but it seems that when the component is rendered the data is not available.我创建了一个与子组件共享的上下文( UserContext ),理想情况下,我希望UserContext.data的值提供表的值,但似乎在呈现组件时数据不可用。 This is visible in the console logs where HERE IS USERCONTEXT {"name":"alice","age":30,"data":{}} is printed a few times, before eventually it prints with the data object being populated.这在控制台日志中可见,其中HERE IS USERCONTEXT {"name":"alice","age":30,"data":{}}打印了几次,然后最终打印出数据 object 被填充。 Currently the NewTable component is getting data for the table via an API call in the component so you can see how the table should look.目前, NewTable组件正在通过组件中的 API 调用获取表的数据,因此您可以查看表的外观。

I would really appreciate advice on if I am making the API call and setting to Context in the correct place, and how I should be rendering that data in child components (in this specific case NewTable, although there will be other components in the future).如果我在正确的位置进行 API 调用并设置为 Context,以及我应该如何在子组件中呈现该数据(在这种特定情况下为 NewTable,尽管将来会有其他组件),我真的很感激建议.

Your context is not available because fetchUser runs asynchronously and has not completed when your NewTable renders.您的上下文不可用,因为fetchUser异步运行并且在您的NewTable呈现时尚未完成。 I don't know what "if I am making the API call and setting to Context in the same place" means, but the approach you're taking is workable.我不知道“如果我在同一个地方进行 API 调用并设置为 Context ”是什么意思,但是您采用的方法是可行的。

If you don't want to render that table before data is loaded, a common approach is to add some mind of "loading" state to your shared context to signal to the child components that data is not ready.如果您不想在加载数据之前呈现该表,一种常见的方法是在您的共享上下文中添加一些“加载”state 的想法,以向子组件发出数据尚未准备好的信号。 You could render some "is loading" or progress bar or something until the data becomes loaded.您可以渲染一些“正在加载”或进度条或其他东西,直到数据被加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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