Next.js Course
Next.js
/
Advanced

Data Mutations

Definition

The end-to-end process of changing data on the server (creating, updating, deleting) and ensuring the client UI perfectly reflects those changes.

Explain Like I'm New

If you add a new 'Todo' item to the database, the database updates, but the user's screen will still show the old list. You must 'mutate' the data, and then immediately tell Next.js to purge the cache and refresh the screen.

Real World Example

A user clicks 'Delete' on an article. The Server Action deletes the row in PostgreSQL, and then calls `revalidatePath('/articles')` to instantly refresh the UI so the article visually disappears.

Common Use Cases

  • •CRUD Operations
  • •Interactive applications

Interactive Example

Loading Code Playground...

Interview Questions

basic

  • What is the primary Next.js function used to refresh the UI after a Server Action mutates data?

intermediate

  • How do you show a 'Loading...' state on a submit button while a Server Action mutation is running?

Flash Cards

Question

Which function?

Click to reveal answer
Answer

`revalidatePath()` or `revalidateTag()`.

Question

Show loading state?

Click to reveal answer
Answer

You must use the `useFormStatus()` hook from `react-dom` inside a Client Component button.