javascript - 如何防止 React hooks 在加载时删除以前的数据?

标签 javascript reactjs react-hooks react-apollo flicker

我正在尝试 react Hook 并制作我的第一个组件。一切正常,除了加载数据时出现闪烁问题,这是我的组件(一个简单的文章列表,带有上一页/下一页):

function InfoFeed ({ limit, uuid }) {
  const [currentIndex, setCurrentIndex] = useState(1)
  const { data, loading } = useQuery(gql(queries.InfoFeed), {
    variables: {
      offset: (articlesByPage * (currentIndex - 1))
    }
  })
  const { articles } = data

  const incrementCb = () => setCurrentIndex(currentIndex === maxPaginationIndex ? 1 : currentIndex + 1)
  const decrementCb = () => setCurrentIndex(currentIndex === 1 ? maxPaginationIndex : currentIndex - 1)

  return (
    <Panel title="Fil Info">
      <StyledList>
        {articles && articles.map((article, index) => (<InfoItem key={index} article={article}/>))}
      </StyledList>
      <button onClick={incrementCb}>next</button>
      <button onClick={decrementCb}>prev</button>
    </Panel>
  )
}

问题是:当点击 prev/next 按钮时,它会启动服务器请求,loading 自动设置为 true,并且 data 设置为 undefined。数据未定义,加载时文章列表为空。我可以使用 loading 变量显示微调器条件,但这是一个非常快的请求(不需要微调器),并且它不会阻止以前的数据消失,这是这种不需要的闪烁的原因.

知道如何处理这个吗?谢谢!

最佳答案

通过迁移到官方@apollo/react-hooks 绑定(bind)解决,因为我使用的是 https://github.com/trojanowski/react-apollo-hooks

关于javascript - 如何防止 React hooks 在加载时删除以前的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57434157/

相关文章:

javascript - 替换 RSS 阅读器 (InoReader) 中的 Facebook 缩略图

javascript - Intl.NumberFormat 返回数字和货币符号除以空格的结果

javascript - 如何初始化阿拉伯语/波斯语键盘的文本输入?

javascript - 未捕获的 TypeError : path. split 不是 react 中的函数

reactjs - 即使添加到 useEffect 中,从自定义 Hook 发出的状态更改也不会导致重新渲染

Javascript 未出现在 Rails 4 中。(Highchart)

javascript - 如何在 TypeScript 中使用 navigation.replace?

javascript - 看不到更新状态

javascript - Material UI React 测试用例失败 - JEST、ENZYME

javascript - 从 React Hook 复制一个数组