简体   繁体   English

如果我在render()之前转换数据,React-apollo更新不会在变异后重新加载

[英]React-apollo update not reloading after mutation if I transform data before the render()

I have wrapped for both Query and Mutations so I can globally handle the repeat actions that need to happen with each Query, Mutation . 我已经包装了Query和Mutations,所以我可以全局处理每个Query, Mutation需要发生的重复操作。 In the query I transform the data so I don't need to worry about all the nodes, edges, etc 在查询中我转换数据,所以我不需要担心所有的nodes, edges, etc

I am using react-adopt to wrap all my query and mutations components into one render prop back on the view layer. 我正在使用react-adopt将我的所有querymutations组件包装回视图层上的一个渲染道具。

Works - Page will re-render once a mutation has taken place Works - 一旦发生突变,页面将重新渲染

<ApolloQuery>

export const ApolloQuery = ({
  query: query,
  render,
}) => {
  return (
    <Query query={query}>
      {({ data }) => {
        return (
          <Fragment>
               render(data)
          </Fragment>
        )
      }}
    </Query>
  )
}

A Component 一个组件

export default externalProps => {
  return (
    <QueryContainer {...externalProps}>
      {({ someQueryData, aMutation }) => { //react-adopt render props
        const { nestedData } = new apolloClass(someQueryData).start()
        return (
          <Grid container spacing={16}>
            {nestedData.map((ticket, index) => (
               {...Mutation button in here}
            ))}
         </Grid>
        )
      }}
    </QueryContainer>
  )
}

Does not work - Page does not re-render but cache is updated with correct records 不起作用 - 页面不会重新渲染,但缓存会使用正确的记录进行更新

<ApolloQuery>

    <Query query={query}>
      {({ data }) => {
        const transformedData = new apolloClass(data).start() //move transform into render
        return (
          <Fragment>
               render(transformedData)
          </Fragment>
        )
      }}
    </Query>

A Component 一个组件

export default externalProps => {
  return (
    <QueryContainer {...externalProps}>
      {({ someQueryData: { nestedData }, aMutation }) => {
        return (
          <Grid container spacing={16}>
            {nestedData.map((ticket, index) => (
               {...Mutation button in here}
            ))}
         </Grid>
        )
      }}
    </QueryContainer>
  )
}

So now, the page will not update after a mutation if I move the apolloClass to transform before the render of the query 所以现在,如果我在query呈现之前移动apolloClass进行转换,页面将不会在突变后更新

您很可能需要在突变选项中设置refetchQueriesawaitRefetchQueries以强制Apollo更新这些查询,从而触发重新渲染。

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

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