How to reset Redux toolkit query state (query, not mutation)?

See original GitHub issue

In Redux toolkit query we write as follows in mutations:

const [purchase, { isLoading, isError, isSuccess, reset }] = useCreateOneSaleMutation()

and we can use the reset method to reset the state.

Is there a way I can do the same in the query hooks?

const { isLoading, isError, isSuccess, reset } = useGetOneSaleQuery()
  • we can do refetch which actually exists, but I need something to reset these states in a component on call, so I need a reset method.

Is it supported in redux toolkit query?

I depend on isSuccess in a useEffect to render some logic, and I need this isSuccess to reset back to false when the component unmounts.

I have a modal, and when this modal closes, it should reset back to false, because the useEffect looks as follows:

useEffect(()=>{
   switch(true){
      case isSuccess:
        setText('Transaction completed successfully!')
      break
      default:
        setText('Please Enter The client id')
   }
}, [isSuccess])

so there must be something like

useEffect(()=> ()=> reset())

in order for this kind of logic to work.

asked on stackoverflow as well https://stackoverflow.com/q/74829321/20779272

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Dec 17, 2022

@samislam : “invalidate” has a very specific meaning, which is “force RTKQ to go try to refetch the data”. It sounds like what you want is to remove the cache entry instead?

Reading the initial description, you said:

need this isSuccess to reset back to false when the component unmounts.

I’m not sure that’s a good way to think about the logic. Can you show more of the actual component / relevant code?

1reaction
phryneascommented, Dec 16, 2022

By the way, what I referenced above:

const [timeOfMount] = useState(new Date().getTime())
const result = useSomeQuery({ at: timeOfMount})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to reset Redux toolkit query state (query, not mutation)?
1 Answer 1 ... RTK Query handles this sort of query-resetting through "tags". Check out the docs section on Automated Re-fetching. Where you ......
Read more >
Mutations
RTK Query > Usage > Mutations: sending updates to the server. ... reset - A method to reset the hook back to it's...
Read more >
Queries
For anything that alters data on the server or will possibly invalidate the cache, you should use a Mutation. By default, RTK Query...
Read more >
Manual Cache Updates
You should generally avoid manually updating the cache outside of the onQueryStarted callback for a mutation without a good reason, as RTK Query...
Read more >
Automated Re-fetching
RTK Query > Usage > Automated Refetching: cache invalidation ... may be undefined based on whether the mutation was successful or not.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found