Skip to content

Commit

Permalink
docs: vike-react-query minor
Browse files Browse the repository at this point in the history
  • Loading branch information
nitedani committed Jan 29, 2024
1 parent 850f1cd commit 1802f36
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions packages/vike-react-query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ If used together with `telefunc`, the query function will always run on the serv
#### Query example:
```tsx
//movie.telefunc.ts
export const function getMovie(id: string) {
export async function getMovie(id: string) {
const movie = await prisma.movie.findUnique({ where: id })
return movie;
}
Expand All @@ -104,12 +104,12 @@ import { getMovie } from './movie.telefunc'

const Movie = withFallback(
({ id }: { id: string }) => {
const result = useSuspenseQuery({
const query = useSuspenseQuery({
queryKey: ['movie', id],
queryFn: () => getMovie(id)
})

const { title } = result.data
const { title } = query.data

return (
<div>
Expand Down Expand Up @@ -144,22 +144,22 @@ import { createMovie } from './movie.telefunc'

const CreateMovie = () => {
const ref = useRef<HTMLInputElement>(null)
const methods = useMutation({
const mutation = useMutation({
mutationFn: createMovie
})

const onCreate = () => {
const title = ref.current?.value || 'No title'
methods.mutate({ title })
mutation.mutate({ title })
}

return (
<div>
<input type="text" ref={ref} />
<button onClick={onCreate}>Create movie</button>
{methods.isPending && 'Creating movie..'}
{methods.isSuccess && 'Created movie ' + methods.data.title}
{methods.isError && 'Error while creating the movie'}
{mutation.isPending && 'Creating movie..'}
{mutation.isSuccess && 'Created movie ' + mutation.data.title}
{mutation.isError && 'Error while creating the movie'}
</div>
)
}
Expand Down Expand Up @@ -193,7 +193,8 @@ const Movies = withFallback(
const mutation = useMutation({
mutationFn: createMovie,
onSuccess() {
queryClient.invalidateQueries({ queryKey: ['movies'] })
query.invalidateQueries({ queryKey: ['movies'] })
// or query.refetch()
}
})

Expand Down Expand Up @@ -229,6 +230,4 @@ const Movies = withFallback(
)
}
)


```

0 comments on commit 1802f36

Please sign in to comment.