简体   繁体   English

在哪里调用 RTKQ useMutation() 的重置方法?

[英]Where to call reset method for RTKQ useMutation()?

Since RTK 1.7.0, mutation result object has a reset() method to unsubscribe from the state update of a mutation from another component:从 RTK 1.7.0 开始,突变结果 object 有一个reset()方法来取消订阅来自另一个组件的突变的 state 更新:

const [mutate, { isLoading, reset }] = useMutation({ fixedCacheKey });

I use it to get isLoading state from another component's mutation.我用它从另一个组件的突变中获取isLoading state。 But I don't know where to call the cleanup reset() :但我不知道在哪里调用清理reset()

  • In the click handler:在点击处理程序中:

     await mutate() reset()
  • Or in the cleanup或者在清理

    useEffect(() => () => reset(), [reset])

    If it's this case, then do I need to do this for both (original and subscriber) components?如果是这种情况,那么我是否需要对两个(原始和订阅者)组件都执行此操作? And this is weird: the identity of reset function changes after state update, so the isLoading is cleared right after it changes.这很奇怪: reset function 的身份在 state 更新后发生了变化,所以isLoading在它发生变化后立即被清除。

I think the docs about this function is not very clear.我认为关于这个 function 的文档不是很清楚。 Hope someone can improve it.希望有人可以改进它。

I'm using React 17.0.2, RTK 1.7.1.我正在使用 React 17.0.2、RTK 1.7.1。

Mutations never share state between usage in different components in the fist place - unless you force that by using fixedCacheKey .突变永远不会在第一处的不同组件中的使用之间共享 state - 除非您使用fixedCacheKey强制这样做。 Are you maybe trying to solve a non-problem here?你可能想在这里解决一个非问题吗?

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

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