aribouius / jsonapi-react

A minimal JSON:API client and React hooks for fetching, updating, and caching remote data.
MIT License
149 stars 28 forks source link

How to fetch data and don't update the results after mutation #48

Closed thiagonzalez closed 2 years ago

thiagonzalez commented 2 years ago

Hello guys,

I have a page that needs to verify if any applications were created by the user to show or not a content. If the user has one or more applications, I redirect him to the applications page. If the user doesn't have application, a tutorial will start to create a first application.

When the user creates an application using the tutorial, the lib refetches data and redirect the user, because now he has an application, but the tutorial still has some additional steps after this creation.

Can I prevent refetching applications in this situation? You can find the snippet below:

export default function Dashboard() {
  const { data, isLoading } = useQuery(['apps']);

  if (isLoading) return <Loading />;

  if (data.length > 0) {
    /* function to redirect users here */
    return <Loading />;
  }

  return <Tutorial />;
}
aribouius commented 2 years ago

Hi @thiagonzalez,

Yes this is just requires instructing the mutation hook to not attempt to perform any cache invalidation.

const [mutation] = useMutation('apps', {
  invalidate: false
})

Let me know if that works for you!

thiagonzalez commented 2 years ago

hey @aribouius, thanks for your response, but the param invalidate doesn't accept boolean, only string or array of strings:

image

aribouius commented 2 years ago

@thiagonzalez ah sorry about that, I'm not a TS user so I hadn't noticed this before. The invalidate option is intended to allow a falsey value (e.g. false and null), but the types config wasn't wired up correctly.

Would you be comfortable submitting a PR that updates the types?

thiagonzalez commented 2 years ago

@aribouius thanks for the opportunity to contribute to your project! Please, have a look at my PR. I added an explanation on the README and also added boolean as one of the types for invalidate.

aribouius commented 2 years ago

Thanks for your contribution @thiagonzalez, and apologies for the delay in getting your PR merged, I didn't have much time to work on this over the holidays.

Your update is now available on version 0.0.25.

thiagonzalez commented 2 years ago

No worries at all, @aribouius. Thank you so much for your incredible work here! Happy new year! :)