How to reset Redux toolkit query state (query, not mutation)?
See original GitHub issueIn 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
refetchwhich 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:
- Created 9 months ago
- Comments:17 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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:
I’m not sure that’s a good way to think about the logic. Can you show more of the actual component / relevant code?
By the way, what I referenced above: