Mutations
Learn how to mutate data on the server.
As we saw in adding new endpoint we can define mutations in the same way as queries.
Of course, as for the queries, we can run mutations on the server-side or client-side, let's see how it works.
Server actions
In Next.js, the common approach to perform mutations is to use server actions. They're especially useful for form submissions.
With our api
it's as simple as possible - it's just a function call performed on the server.
For example, you can define an action to create a post like this:
In the above example we're also using revalidatePath
to revalidate the path /posts
to fetch the updated list of posts.
useMutation hook
On the other hand, if you want to perform a mutation in the client-side, you can use the useMutation
hook that comes straight from the integration with Tanstack Query.
Last updated on