简体   繁体   English

中继graphql片段有什么好处?

[英]What is benefit of relay graphql fragment?

I have months of experience of using relay graphql.我有数月的使用中继 graphql 的经验。 I continue the project from another dev that is leaving, I use one QueryRenderer on every page and pour the data fetched from the main query as a plain object as props to every child component, I see no problem here.我从另一个即将离开的开发人员那里继续该项目,我在每个页面上使用一个 QueryRenderer 并将从主查询中获取的数据作为普通对象作为道具倒入每个子组件,我认为这里没有问题。 But I see people are using fragments and suggesting fragments heavily.但是我看到人们正在使用片段并大量建议片段。 I think it's just more typings and real waste.我认为这只是更多的打字和真正的浪费。 I don't really understand, we can just put the data as a prop to every child component, so why people really need a fragment?我真的不明白,我们可以把数据作为每个子组件的道具,那么为什么人们真的需要一个片段? is there a technical benefit that I miss?有我想念的技术优势吗?

It helps you to maintain your code.它可以帮助您维护代码。

If you have a Page with 3 different components and every component need different data than you can specify the needed data for each component within a fragment inside the component.如果您有一个包含 3 个不同组件的页面,并且每个组件都需要不同的数据,那么您可以在组件内部的片段中为每个组件指定所需的数据。 If you later want to get rid of one of the 3 components, you just need to remove the fragment for this component form your QueryRenderer instead of checking which params you can remove from your QueryRenderer.如果您以后想要摆脱 3 个组件之一,您只需从 QueryRenderer 中删除该组件的片段,而不是检查可以从 QueryRenderer 中删除哪些参数。

By specifying the needed data in the component it is much easier to be sure to have the correct and only the necessary data fetched.通过在组件中指定所需的数据,更容易确保获取正确且仅必要的数据。 If 3 different developers write the components (each of them one), it is easy to write the QueryRenderer for the page.如果由 3 个不同的开发人员编写组件(每人一个),则很容易为页面编写 QueryRenderer。 Because instead of checking the data each component needs and passing the individual params to the components, you just specify the 3 fragments in the QueryRenderer and it will work.因为不是检查每个组件需要的数据并将单独的参数传递给组件,您只需在 QueryRenderer 中指定 3 个片段,它就会起作用。

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

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