Next.js · Middle · Question 71 of 75

How do you call a Server Action from a button's onClick (not a form) and show pending state?

const [isPending, startTransition] = useTransition()
<button onClick={() => startTransition(() => likePost(id))}>

Explanation

The transition gives isPending and lets Next.js update the UI with revalidated data from the action's response.

Further reading

Problem with a question

Next.js · Middle · question 71

Test yourself

Take the Next.js · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz