简体   繁体   English

如果我们使用 loadQuery 和 usePreloadedQuery vs loadQuery 和 useLazyLoadQuery 挂钩,性能将如何影响反应中继?

[英]How performance will impact with react relay if we use loadQuery and usePreloadedQuery vs loadQuery and useLazyLoadQuery hook?

  1. loadQuery and usePreloadedQuery - This combination will actually be performant because we are using reference returned by loadQuery in the usePreloadedQuery hook so this follows the "render-as-you-fetch" pattern. loadQuery 和 usePreloadedQuery - 这种组合实际上是高性能的,因为我们在 usePreloadedQuery 挂钩中使用 loadQuery 返回的引用,因此这遵循“获取时呈现”模式。
  2. loadQuery and useLazyLoadQuery - If we use this combination then also it is giving the same performance as that of 1st combination. loadQuery 和 useLazyLoadQuery - 如果我们使用这个组合,那么它也会提供与第一个组合相同的性能。 According to documentation we should use usePreloadedQuery query hook with loadQuery.根据文档,我们应该使用带有 loadQuery 的 usePreloadedQuery 查询挂钩。 When we useLazyLoadQuery with loadQuery then it does not make an additional API call because it uses the same data which we fetched from loadQuery.当我们将 LazyLoadQuery 与 loadQuery 一起使用时,它不会进行额外的 API 调用,因为它使用我们从 loadQuery 获取的相同数据。

So practically both ways are same or different?那么实际上这两种方式是相同的还是不同的? In terms of good architecture what should be the preferred way and why?就良好的架构而言,首选方式应该是什么?为什么?

usePreloadedQuery sends the request while the component is rendering, and can suspend with a loading state if the component is ready before the query returns. usePreloadedQuery组件呈现时发送请求,如果组件在查询返回之前准备就绪,则可以挂起加载 state。

On the other hand useLazyLoadQuery sends the request after the component has rendered.另一方面, useLazyLoadQuery在组件呈现发送请求。

Because API requests generally take longer than React renders, it is better to start fetching as early as possible.因为 API 请求通常比 React 渲染花费的时间更长,所以最好尽早开始获取。 Using prefetching with React.Suspense also improves user experience, because they won't see the component rendering nothing or an empty initial state before becoming populated with data.将预取与React.Suspense一起使用还可以改善用户体验,因为在填充数据之前,他们不会看到组件呈现任何内容或空的初始 state。

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

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