ReactQuery Pass状态将升级为按钮
当单击使用useQuery触发refcter函数时,我尝试将isloadation状态传递给按钮prop,并且它不起作用(也许是因为它重新渲染组件以及组件渲染is Loading为false时)。我想知道如何使它起作用?
const MainPage: React.FC = () => {
const {data, isLoading, error, refetch} = useQuery<any>(
["getCity"],
() => fetchCities(),
{
enabled: false,
}
)
return (
<div>
<div>
{JSON.stringify(data)}
</div>
<Button
loading={isLoading} // always false. Need to be true when start refetch and false by the end refetch
label="get_cities"
onClick={() => refetch()}
>
</Button>
</div>
)
}
export default MainPage;
I try to pass isLoading state to button prop when click button which trigger refetch function from useQuery, and it doesn't work (maybe because it re-render component and when component render isLoading is false). I wonder how i can make it works?
const MainPage: React.FC = () => {
const {data, isLoading, error, refetch} = useQuery<any>(
["getCity"],
() => fetchCities(),
{
enabled: false,
}
)
return (
<div>
<div>
{JSON.stringify(data)}
</div>
<Button
loading={isLoading} // always false. Need to be true when start refetch and false by the end refetch
label="get_cities"
onClick={() => refetch()}
>
</Button>
</div>
)
}
export default MainPage;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
isloading
才能成为true
仅当查询具有 没有数据 时,并且当前在iSfetching
>如果查询在任何时候提取(包括背景恢复),将成为true
。在您的情况下,只需使用
iSfetching
而不是isloading
isLoading
will becometrue
only when query has no data and is currently fetching whileisFetching
will becometrue
if the query is fetching at any time (including background refetching).In your case, just use
isFetching
instead ofisLoading