react-query-auth icon indicating copy to clipboard operation
react-query-auth copied to clipboard

refetchUser function example/ invalidate auth-user

Open icobg123 opened this issue 2 years ago • 1 comments

Hi,

Can you please provide an example of how to force a user refetch? The docs mention a refetchUser function but that is not used anywhere in the demo. I've tried invalidating the query key after a mutation but that doesn't force a refetch either.

Thanks!

icobg123 avatar Aug 16 '22 12:08 icobg123

You just need to call the refetchUser() function. So, something likes this:

const { user, refetchUser } = useAuth();

const myFunction = async() => {
    try {
        await myApiCall();
        
        // if  the result of the above function is success, user data is changed and we want to refetch the user 
        await refetchUser();
    } catch (error) {
        //...
    }
}

What happens here is that refetchUser() invokes the loadUser() function that you have to create in your auth provider.

stefanboz avatar Sep 12 '22 07:09 stefanboz