哪种状态表示在React-Query中使用useQuery启用时尚未获取的加载或数据?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我首先说V4中有一个更改,因此让我们分别查看两个主要版本:
v3中的V3
,没有数据并且当前没有获取数据的查询在
状态中:'idle:' '
或isidle:true
(如V4
在V4中,闲置状态已被删除,并添加了“次要”状态,该状态使这两个状态的所有排列成为可能:
状态:'loading'| “成功” | '错误'
此状态为您提供有关
数据
字段的信息,可用于类型:加载
:尚无数据成功>
:有数据错误
:没有数据,因为以前的获取状态中存在错误,与状态非常相似:<代码>待处理 | <代码>实现 |
拒绝
。fetchstatus:'idle'| '提取'| “暂停”
此状态为您提供有关当前是否正在获取React-Query的信息(可能是第一个获取或随后的倒口气)。
正如我说的,所有组合都是可能的。如果尚无数据的禁用查询,您将处于
状态:'loading'
和fetchstatus:'idle'
。在您的具体示例中,使用
占位符
的使用会大大改变这些状态。由于使用lastholderData
,您实际上具有 data 渲染,因此状态将始终为success
(因为这仅表示有可用的数据) 。还有一个附加的iSplacholderData
从useQuery
返回的标志,您可以用来确定您是否具有假,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'
orisIdle: 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 yetsuccess
: there is dataerror
: there is no data because there was an error in the previous fetchThis 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'
andfetchStatus: 'idle'
.In your specific example, the use of
placeholderData
changes those states significantly. Since when usingplaceholderData
, you actually have data to render, the status will always besuccess
(as that only indicates that there is data available). There is an additionalisPlacholderData
flag returned fromuseQuery
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, thefetchStatus
will then additionally be'idle'
for disabled queries, and'fetching'
for enabled queries.您正在寻找
isidle
:You are looking for
isIdle
:当
启用时:false
获得正确的加载状态,可以测试when
enabled: false
to get the right loading state you can test on