哪种状态表示在React-Query中使用useQuery启用时尚未获取的加载或数据?

发布于 2025-02-04 08:48:31 字数 1771 浏览 2 评论 0原文

const {isAuthenticated} = useAuth();

    const { isLoading: isTwilioAccountsLoading, isSuccess: isTwilioAccountsSuccess, isError: isTwilioAccountsError, data: twilioAccounts, error: errorTwilioAccounts } = useQuery("twilioAccounts", () => TwilioAccountsFunc(), {
        enabled: !!isAuthenticated,
        placeholderData: []
    });

    function getTwilioAccounts() {
        if (!isTwilioAccountsSuccess) {
            return <div className="flex flex-col items-center w-1/4">
                <div className="mt-4">
                    <p className="w-24"><Skeleton /></p>
                </div>
            </div> 
        } else {
            if (isTwilioAccountsSuccess && twilioAccounts && twilioAccounts.length > 0) {
                return twilioAccounts.map((account, i) => {
                    return <div key={i} className="flex flex-col items-center w-1/4">
                        <div className="mt-4">
                            {!account.is_subaccount ?
                            <Link to={"/accounts/twilio/" + account.account_id} className="underline text-gray-800 font-medium">{account.name}</Link> :
                            <p className="leading-tight font-medium text-gray-800">{account.name}</p>
                            }
                        </div>
                    </div>
                });
            } else {
                return <Alert message="There are currently no accounts in use." />
            }
        }
    }


当我们仍然需要获取twilioAccounts数据时,我想在初始页面加载的骨架上显示该部分。因为我使用启用依赖项,所以iSloading为false,iSSUCCESS在初始页面加载中是正确的。我应该使用什么状态来捕获可用的数据?

const {isAuthenticated} = useAuth();

    const { isLoading: isTwilioAccountsLoading, isSuccess: isTwilioAccountsSuccess, isError: isTwilioAccountsError, data: twilioAccounts, error: errorTwilioAccounts } = useQuery("twilioAccounts", () => TwilioAccountsFunc(), {
        enabled: !!isAuthenticated,
        placeholderData: []
    });

    function getTwilioAccounts() {
        if (!isTwilioAccountsSuccess) {
            return <div className="flex flex-col items-center w-1/4">
                <div className="mt-4">
                    <p className="w-24"><Skeleton /></p>
                </div>
            </div> 
        } else {
            if (isTwilioAccountsSuccess && twilioAccounts && twilioAccounts.length > 0) {
                return twilioAccounts.map((account, i) => {
                    return <div key={i} className="flex flex-col items-center w-1/4">
                        <div className="mt-4">
                            {!account.is_subaccount ?
                            <Link to={"/accounts/twilio/" + account.account_id} className="underline text-gray-800 font-medium">{account.name}</Link> :
                            <p className="leading-tight font-medium text-gray-800">{account.name}</p>
                            }
                        </div>
                    </div>
                });
            } else {
                return <Alert message="There are currently no accounts in use." />
            }
        }
    }


I want to show the section with the skeleton on the initial page load when we still need to fetch the twilioAccounts data. Because I am using the enabled dependency, the isLoading is false and isSuccess is true on the initial page load. What state should I be using to capture no data available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

围归者 2025-02-11 08:48:31

让我首先说V4中有一个更改,因此让我们分别查看两个主要版本:

v3中的V3

,没有数据并且当前没有获取数据的查询在状态中:'idle:' 'isidle:true(如

V4

在V4中,闲置状态已被删除,并添加了“次要”状态,该状态使这两个状态的所有排列成为可能:

  • 状态:'loading'| “成功” | '错误'

此状态为您提供有关数据字段的信息,可用于类型:

  • 加载:尚无数据
  • 成功>:有数据
  • 错误:没有数据,因为以前的获取状态中存在错误

,与状态非常相似:<代码>待处理 | <代码>实现 | 拒绝

  • fetchstatus:'idle'| '提取'| “暂停”

此状态为您提供有关当前是否正在获取React-Query的信息(可能是第一个获取或随后的倒口气)。

正如我说的,所有组合都是可能的。如果尚无数据的禁用查询,您将处于状态:'loading'fetchstatus:'idle'


在您的具体示例中,使用占位符的使用会大大改变这些状态。由于使用lastholderData,您实际上具有 data 渲染,因此状态将始终为success(因为这仅表示有可用的数据) 。还有一个附加的iSplacholderDatauseQuery返回的标志,您可以用来确定您是否具有假,Placholter Data(true)或“真实”数据(false)。这两个版本都是如此。在v4中,fetchstatus还将为禁用查询'idle',而'fetching'用于启用查询。

Let me start by saying that there is a change coming in v4 to this, so let's look at the two major versions separately:

v3

in v3, queries that have no data and are not currently fetching data are in status: 'idle' or isIdle: true (as mentioned in @jakub-kotrs answer)

v4

in v4, the idle state has been removed and a "secondary" state has been added that makes all permutations of the two states possible:

  • status: 'loading' | 'success' | 'error'

This state gives you information about the data field and can be used for type-narrowing:

  • loading: there is no data yet
  • success: there is data
  • error: there is no data because there was an error in the previous fetch

This status is pretty similar to the states a Promise can be in: pending | fulfilled | rejected.

  • fetchStatus: 'idle' | 'fetching' | 'paused'

This state gives you information about if react-query is currently fetching (could be the first fetch, or a subsequent refetch).

As I said, all combinations are possible. In case of a disabled query that doesn't have data yet, you'll be in status: 'loading' and fetchStatus: 'idle'.


In your specific example, the use of placeholderData changes those states significantly. Since when using placeholderData, you actually have data to render, the status will always be success (as that only indicates that there is data available). There is an additional isPlacholderData flag returned from useQuery that you can use to determine if you have fake, placholder data (true) or "real" data (false). This is true for both versions. In v4, the fetchStatus will then additionally be 'idle' for disabled queries, and 'fetching' for enabled queries.

最终幸福 2025-02-11 08:48:31

您正在寻找isidle

const {isIdle} = useQuery(["key"], fn, {
    enabled: false
});

console.log(isIdle)

You are looking for isIdle:

const {isIdle} = useQuery(["key"], fn, {
    enabled: false
});

console.log(isIdle)
染墨丶若流云 2025-02-11 08:48:31

启用时:false获得正确的加载状态,可以测试

fetchStatus === 'fetching' && status === 'loading'

when enabled: false to get the right loading state you can test on

fetchStatus === 'fetching' && status === 'loading'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文