简体   繁体   English

接力createFragmentContainer的现代目的

[英]Relay Modern purpose of createFragmentContainer

I've been trying relay-modern for some time, and I'm wondering what are the purposes of createFragmentContainer other than just for describing the fragment that should beloing to the Component . 我一直在尝试relay-modern一段时间,我想知道createFragmentContainer的目的是什么,而不仅仅是为了描述 应该Component的片段。

eg this is how documentation show how it is supposed to be 例如,这就是文档如何显示它应该如何

Parent.js Parent.js

<QueryRenderer
  render={({error, props}) => {
    if (error || props) {
       return <Child someData={someData}>
    }
    return <div>Loading</div>
  }}

  query={graphql`
    query SomeQuery($id: ID!) {
      endpoint(id: $id) {
        ...Child_someData
      }
    }
  `}
/>

Child.js Child.js

export default createFragmentContainer( 
  ({someData}) => <header>{someData.title} - {someData.name}</header>,
  graphql`
    fragment Child_someData on EndPoint {
       title
       name
    }
  `
)

But instead doing Child.js in that way, I can just rewrite or splitting the component with query to 2 different files like this: 但是以这种方式做Child.js ,我可以用查询重写或拆分组件到2个不同的文件,如下所示:

ChildComponent.js ChildComponent.js

export default ({someData}) => <header>{someData.title} - {someData.name}</header>

Child.js Child.js

export default graphql`
  fragment Child_someData on EndPoint {
    title
    name
  }
`

and it is still going to work ( Parent.js will still recognize the fragment). 并且它仍然可以工作( Parent.js仍将识别该片段)。 So this makes me wondering if createFragmentContainer just for syntactic sugar to make things tidier. 所以这让我想知道createFragmentContainer只是为了使语法变得更加整洁。

If anyone can shed a light with this one, that would be awesome! 如果有人能用这个灯光照亮,那就太棒了! can't find so much in the documentation about this 文档中找不到这么多

Your example if a fairly static implementation... I think what you need to consider is that these are containers that provide additional functionality, fragmentContainer being one of the more abstract layers. 你的例子是一个相当静态的实现......我认为你需要考虑的是这些是提供附加功能的容器, fragmentContainer是更抽象的层之一。

I would look more at how the refetchContainer and paginationContainer expand upon the idea of a fragmentContainer and also look at the Github repository itself, 我会更多地看看refetchContainerpaginationContainer如何扩展fragmentContainer的想法,并查看Github存储库本身,

https://github.com/facebook/relay/blob/master/packages/react-relay/modern/ReactRelayFragmentContainer.js https://github.com/facebook/relay/blob/master/packages/react-relay/modern/ReactRelayFragmentContainer.js

So certainly child containers could simply be a file filled with fragments that you are exporting out, but ideally you should think of them as containers which are extensions of React components. 因此,子容器当然可以只是一个填充了要导出的片段的文件,但理想情况下,您应该将它们视为React组件扩展的容器。 They are container fragments that bubble up into a compositional query which afford you conveniences for managing state in the context of React. 它们是容器碎片,冒泡到组合查询中,为您提供在React环境中管理状态的便利。

Relay compiler will find your Child.js file so the fragment will be created and your query will be fetched. 中继编译器将找到您的Child.js文件,以便创建片段并获取您的查询。 However, the difference is createFragmentContainer is a HOC that Relay uses to guarantee that the component won't render until all necessary data is available. 但是,区别在于createFragmentContainer是一个HOC,Relay使用该HOC来保证在所有必要数据可用之前,组件不会呈现。 This is the purpose of FragmentContainer and that's why you should use it. 这是FragmentContainer的目的,这就是你应该使用它的原因。

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

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