我正在尝试 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/