Flag to prioritize mutation.mutate()'s side effects #2020
Unanswered
zachdtaylor
asked this question in
Ideas
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Here's my situation:
I've built a very basic contact book app. On the main page, you can see a list of contacts. When you click on a contact, you can see details about that contact, and you can delete that contact. Upon deleting the contact, you are automatically re-routed back to the main page.
On the contact detail page, I'm using a query to get the contact information:
and a mutation to delete the contact:
And here's the component (for reference, I'm using Next.js):
When the user clicks the delete button,
handleDelete
is called, which calls thedeletePerson.mutate()
function and pushes a new route onto the router on success. Because I don't want stale queries in the cache, you'll notice that I callqueryClient.removeQueries
in the mutation'sonSuccess
handler.The problem is that the query actually doesn't get deleted from the cache. Well, it does... but not before a new instance of the query is mounted with blank data (because of the
usePerson(id)
).So the flow is: user clicks delete ->
mutation.mutate()
is called ->queryClient.removeQueries()
is called -> new instance of query mounts ->router.push("/")
I realize I could just move the
router.push("/")
into theuseDeletePerson
hook, right before the call toremoveQueries
, but I like to keep UI effects in my components, separate from data effects, so I'd prefer not to do that.As of right now, I don't think there would be a way to call
router.push("/")
beforeremoveQueries
while keeping them separated. Maybe we could add a flag somewhere to allow you to prioritizemuatate()
's instance ofonSuccess
?Beta Was this translation helpful? Give feedback.
All reactions