【问题标题】:I want to update every 3 seconds without using react apollo refetch我想每 3 秒更新一次,而不使用 react apollo refetch
【发布时间】:2021-11-11 00:04:45
【问题描述】:

我正在使用反应阿波罗。
我想在不使用 apllo 的 refetch 的情况下每 3 秒更新一次进度条组件的 user.percent。

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

【问题讨论】:

    标签: reactjs typescript apollo


    【解决方案1】:

    您可以使用Apollo's polling feature 每隔 X 毫秒执行一次查询。

      const {data: {user = null} = {}, startPolling, stopPolling} = useUserQuery({
          variables: {uuid: userId},
          skip: !userId,
          pollInterval: 3000, // <-- refetch every 3 sec
    
        });
    

    您可以分别使用startPollingstopPolling 函数启动和暂停/停止轮询。

    【讨论】:

    • 如何使用 startPolling 和 stopPolling?
    • 像普通函数一样?您需要解释这个非常笼统的问题是什么意思。此外,您最初的问题只是需要另一种方式来重复拨打此答案所回答的电话。
    • 如果您认为此答案对您有所帮助,请考虑upvoting and accepting the answer。我看到了你的个人资料,但你似乎不接受或不赞成答案,这将阻止用户在未来回答你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2022-01-13
    • 2015-06-22
    • 1970-01-01
    • 2014-11-24
    • 2015-04-16
    相关资源
    最近更新 更多