首页 文章

React-Apollo Query组件变量永远不会更新

提问于
浏览
1

我正在尝试将图表组件包装在检索图表数据的 <Query> 组件中 . 图表组件使用 QueryfetchMore 渲染道具通过更新查询的变量将新数据附加到图表 .

The problem I'm having is that the Query component's render prop never seems to update the variables argument after fetchMore is run with an updated variables argument.

这是 react-apollo 的错误,还是我错误地使用_1435613?

这是图表的渲染功能:

render() {
  return (
    <Query
      query={gql`
        query Chart($from: Long!, $to: Long!) {
          chartData(from: $from, to: $to) @connection(key: "chartData") {
            ...
          }
        }
      `}
      variables={{ from: this.props.from, to: this.props.to }}
    >
      {({ data: { chartData } = {}, variables: { from, to }, fetchMore }) => {

        const fetchNext = () =>
          fetchMore({
            variables: { from: from + 30, to: to + 30, },
            updateQuery: (previousResult, { fetchMoreResult }) => {
              const updatedResult = Object.assign({}, fetchMoreResult)
              updatedResult.chartData = [
                ...previousResult.chartData,
                ...fetchMoreResult.chartData,
              ]
              return updatedResult
            },
          })

        return (
          <Chart
            chartData={chartData}
            from={from}
            to={to}
            fetchNext={fetchNext}
          />
        )
      }}
    </Query>
  ) 
}

这是我要经历的步骤:

  • render prop与 from = 0to = 29chartData 作为30个数据点的数组运行

  • 触发fetchMore . 查询再次使用 from = 30to = 59 运行 . 回复带来30个新数据点 . updatedResult是完整的60个数据点 .

  • 渲染道具再次运行 . chartData 是一个包含预期的60个数据点的数组,但仍然是 from = 0to = 29 . 我希望他们分别更新到 3059 . 这会导致图表具有错误的xMin和xMax,因为变量未更新 .

作为一种解决方法,我尝试在 updateQuery 回调中设置 fromto 的组件状态副本,但这会导致 fromto 值与 chartData 不同步,直到Apollo完成更新其存储 .

任何帮助是极大的赞赏 . 谢谢!

1 回答

相关问题