简体   繁体   English

使用列表进行React Relay操作

[英]React Relay manipulation with list

Structure is 结构是

store: (Component) => Relay.QL`
  query StoreQuery {
    store { ${Component.getFragment('store')} },
  }
`,

store: () => Relay.QL`
  fragment on Store {
    activities {
      ${Activity.getFragment('activity')}
    },
  }
`,

activity: () => Relay.QL`
  fragment on Activity {
    name,
    icon,
    color
  }
`,

I am getting props.activities as a list and then I want to manipulate this list for example sort or remove not needed, etc. but I see there relay objects and can't understand how to get data and change it before setting to component ? 我正在获取props.activities作为列表,然后想要操作该列表,例如不需要排序或删除等,但是我看到那里有中继对象,并且在设置为component之前无法理解如何获取和更改数据?

The data passed to each component from relay is masked, so you cannot access it even if another component happens to request it. 从中继传递到每个组件的数据被屏蔽,因此即使另一个组件碰巧请求它,您也无法访问它。 In order to access any field, you have to ask for it. 为了访问任何字段,您必须要求它。

Say, the outer component wants to sort the activities based on the name field. 假设外部组件希望根据name字段对活动进行排序。 You would need to update the fragment to in order to explicitly asked for the name in that component as well. 您还需要将片段更新为,以便也明确要求该组件中的名称。

fragment on Store {
  activities {
    name
    ${Activity.getFragment('activity')}
  },
}

The benefit is that even when the inner fragment removes the name field for whatever reason, the outer component would still have the data. 好处是,即使内部片段出于任何原因删除了name字段,外部组件仍将具有数据。 This is a critical behavior to make it possible to reason about a component locally without knowing about the full application. 这是至关重要的行为,可以在不了解整个应用程序的情况下就本地推理组件。

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

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