4

我有一组来自 graphql 查询的“帖子”,我正在尝试使用apollo docs中描述的 offset + limit 进行分页。当前按下“loadMore”会更新查询,结果fetchMoreResult是正确的。此外,连接newResults时会正确显示。也许我做出了一个不正确的假设,但我理解文档的方式是新结果posts将自动传递给我TestComponent并重新渲染。这是不正确的吗?

import React from 'react';
import { View, Text } from 'react-native';
import { graphql } from 'react-apollo';
import { GetPosts } from 'app/graphql';

class TestComponent extends React.Component {
  render() {
    // I would expect this to log the new props after press "loadMore"
    console.log(this.props)
    return (
      <View style={{marginTop: 40}}>
        <Text onPress={this.props.loadMore}>Push me</Text>
      </View>
    )
  }
}

export default graphql(GetPosts, {
  options: () => ({
    variables: {
      offset: 0,
      limit: 1
    }
  }),
  props: ({ ownProps, data }) => {
    return {
      ...ownProps,
      posts: data.posts,
      loadMore: () => {
        return data.fetchMore({
          variables: {
            offset: 1,
            limit: 1
          },
          updateQuery: (previousResult, { fetchMoreResult }) => {            
            if (!fetchMoreResult) {
              return previousResult;
            }
            const newResult = Object.assign({}, previousResult, {
              posts: [...previousResult.posts, ...fetchMoreResult.posts]
            });
            console.log(newResult);
            return newResult;
          },
        })
      }
    };
  }
})(TestComponent);

编辑(2017 年 4 月 11 日) 我未能解释我的查询GetPosts包含联合类型,这似乎可能是问题,因为如果我删除它们,更新会按预期工作。我已经打开了一个包问题,希望更清楚:https ://github.com/apollographql/react-apollo/issues/602

4

1 回答 1

-2

为了重新渲染,TestComponent您必须更改组件的状态。

你基本上可以做

this.setState({

posts: [old values + new values],

})

然后在渲染函数中,您可以通过以下方式调用它this.state.posts

于 2017-04-04T17:13:12.743 回答