2017-04-04 93 views
2

我有一個來自graphql查詢的「帖子」數組,我試圖在apollo docs中使用offset +極限進行分頁工作。目前按下「loadMore」更新查詢,結果爲fetchMoreResult是正確的。此外,連接的newResults在登錄時顯示正確。也許我做了一個不正確的假設,但我理解文檔的方式是,新生成的posts將自動傳遞到我的TestComponent並重新呈現。這是不正確的?Apollo fetch更新道具不重繪渲染組件

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); 

編輯(17年4月11日) 我無法解釋我的查詢GetPosts包括聯合類型,這似乎像它可能是問題,因爲如果我刪除,更新工作正常。我已經打開包裹的問題,希望更清晰:https://github.com/apollographql/react-apollo/issues/602

+0

調用這個你可以嘗試一些人已經在這裏嘗試過的東西可看:https://github.com/apollographql/react-apollo/issues/549 –

回答

-1

爲了重新渲染TestComponent您已更改組件的狀態。

你基本上可以做到

this.setState({ 

posts: [old values + new values], 

}) 

然後在渲染功能,您可以通過this.state.posts

+1

這個答案是不正確的。 –

+0

新的道具應該由'graphql'高階組件觸發重新渲染。 –