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))}>- ✓Correct answer: Wrap the call in
startTransitionfromuseTransition - 2Server Actions can only be used in
<form action> - 3Call it inside
useEffect
Explanation
The transition gives isPending and lets Next.js update the UI with revalidated data from the action's response.