reactjs - 我想每 3 秒更新一次,而不使用 react apollo refetch
问题描述
我正在使用反应阿波罗。
我想每 3 秒更新一次进度条组件的 user.percent,而不使用 apllo 的重新获取。
import {useInterval} from 'beautiful-react-hooks';
const ProgressBar = () => {
const {userId} = useParams<{userId: string}>();
const {data: {user = null} = {}, refetch: refetchUser} =
useUserQuery({
variables: {uuid: userId},
skip: !userId,
});
if (!user) return null;
useInterval(() => {
refetchUser();
}, 3000);
return (
<p>{user.percent}</p>
)
}
解决方案
您可以使用Apollo 的轮询功能每隔 X 毫秒执行一次查询。
const {data: {user = null} = {}, startPolling, stopPolling} = useUserQuery({
variables: {uuid: userId},
skip: !userId,
pollInterval: 3000, // <-- refetch every 3 sec
});
startPolling
您可以分别使用和stopPolling
函数启动和暂停/停止轮询。
推荐阅读
- amazon-cloudformation - 使用 cloudformation 启动 ec2 实例
- bash - 按多列值拆分 csv 文件并保留标题
- javascript - Google 跟踪代码管理器中的元素可见性触发器显着低于报告
- selenium - 在 Selenium 中设置磁盘缓存大小,同时抓取多个网站?
- rest - Kafka 消息 VS REST 调用
- amazon-web-services - 有关 Amazon Sagemaker groundtruth 的信息
- javascript - 将javascript画布路径保存到数据库中的正确方法
- java - 实现特定数据存储的最佳方法
- javascript - 通过javascript对该数组进行排序的最佳解决方案
- android - 屏幕密度为 440 dpi 的设备与在 Google Play 上发布的应用不兼容