mvasin / react-query-freeze

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This repo illustrates the pitfall with react-query.

Type into the input field, which tries to sync up with the fake backend.

Now try optimistic updates open index.tsx and replace

const { mutate } = useMutation(fakePost, {
  onSuccess() {
    queryClient.invalidateQueries('foo')
  }
})

with

const { mutate } = useMutation(fakePost, {
  onSuccess() {
    queryClient.invalidateQueries('foo')
  },
  async onMutate(data) {
    await queryClient.cancelQueries('foo')
    queryClient.setQueryData('foo', data)
  }
})

We are not even dealing with errors (our fake server doesn't return any errors), but the experience is still... weird.

About


Languages

Language:TypeScript 52.4%Language:HTML 45.7%Language:CSS 1.9%