1. 15
    Manually Refetch Data After an RTK Query Mutation with unwrap() and refetch()
    1m 44s

Manually Refetch Data After an RTK Query Mutation with unwrap() and refetch()

Share this video with your friends

Send Tweet

This lesson is about a powerful combo, refetch and unwrap.

The first and simplest concept is refetch, which forces whatever query you're subscribed to, to pull the latest data from the server. Refetch takes no arguments and doesn't let you change the query in any way. It just forces it to be called again. Another way to manually call refetch is to use initiate on your query's endpoint. For example, api.endpoints.getDogs.initiate(undefined, { forceRefetch: true }) would do the same thing as our refetch call here, but refetch is a lot cleaner.

The second part is around unwrap, which lets you access the original promise result from a mutation, which you can then use to immediately trigger some action, such as refresh. Or, you can use it to display an error message when the mutation fails.