简体   繁体   English

RTK 查询重置

[英]RTK Query reset

I have a MUI dialog component that triggers a useLazyQuery action on save to fetch data.我有一个 MUI 对话框组件,它在保存时触发 useLazyQuery 操作以获取数据。
Then on success dispatches another action然后成功调度另一个动作
My problem is, that after success, I want to reset the state of the useLazyQuery hook in the dialog component, because after success/fail the state is "stuck" like this in the component.我的问题是,成功后,我想重置对话框组件中 useLazyQuery 钩子的状态,因为在成功/失败后,状态在组件中像这样“卡住”。
I am confused how should I approach this...我很困惑我应该如何处理这个......
Dismount and mount the dialog component works but it removes the fadeout animation and also unsubscribe the query endpoint.卸载并安装对话框组件可以工作,但它会删除淡出动画并取消订阅查询端点。
Is there a best practice here that I am missing?这里有我缺少的最佳实践吗?
https://redux-toolkit.js.org/rtk-query/api/created-api/hookshttps://redux-toolkit.js.org/rtk-query/api/created-api/hooks

Hmm, instead of a lazyQuery I'd probably just use a normal query hook with a setState .嗯,我可能只使用带有setState的普通查询钩子,而不是lazyQuery That way you can just skip it.这样你就可以skip它。

const [arg, setArg] = useState(null)
const result = useMyQuery(arg, { skip: arg == null })

And then setArg(null) to "reset" the thing.然后setArg(null)来“重置”这个东西。

So I ended up using RTKQuery without hooks所以我最终使用了没有钩子的 RTKQuery
created a Custom hook and inside used initiate and select创建了一个自定义钩子并在内部使用启动和选择
with useSelector and dispatch,使用 useSelector 和 dispatch,
than I can reset someId when isSuccess is true当 isSuccess 为真时我可以重置 someId

dispatch(api.endpoints.getData.initiate(someId));
const {data, isError, isLoading} = useSelector(state => api.endpoints.getData.select(someId)(state))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM