Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTK Query: manually reset mutation state #1630

Closed
tkharuk opened this issue Oct 21, 2021 · 2 comments
Closed

RTK Query: manually reset mutation state #1630

tkharuk opened this issue Oct 21, 2021 · 2 comments

Comments

@tkharuk
Copy link

tkharuk commented Oct 21, 2021

I'm facing a situation where I need to hide error from mutation.
In React Query there is a mutation.reset method that would do that.

Are there ways to achieve something similar?

const CreateTodo = () => {
   const [create, { error }] = useCreateMutation()
 
   const onCreateTodo = () => {
     create({ /* ... */ })
   }

   const hideError = () => {
     // mutation.reset() ?
   }
 
   return (
     <form onSubmit={onCreateTodo}>
       {error && <h5 onClick={hideError}>{error}</h5>}

        // ...
       <button type="submit">Create Todo</button>
     </form>
   )
 }
@Shrugsy
Copy link
Collaborator

Shrugsy commented Oct 21, 2021

For usage questions, please use the discussions section rather than issues.

A reset method will be available in v1.7 which is not released yet.

In the meantime, @phryneas has shown how you can imitate the functionality yourself in this comment.

e.g.

const CreateTodo = () => {
   const lastMutation = useRef(undefined)
   const [create, { error }] = useCreateMutation()
 
   const onCreateTodo = () => {
     lastMutation.current = create({ /* ... */ })
   }

   const hideError = () => {
     lastMutation.current?.unsubscribe()
   }
 
   return (
     <form onSubmit={onCreateTodo}>
       {error && <h5 onClick={hideError}>{error}</h5>}

        // ...
       <button type="submit">Create Todo</button>
     </form>
   )
 }

@tkharuk tkharuk closed this as completed Oct 21, 2021
@tkharuk
Copy link
Author

tkharuk commented Oct 21, 2021

@Shrugsy thanks for links!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants