useFormStatus pending state not updating during manual Server Action invocation
0 reputation · 29 Mar 2025, 16:13 UTC
0 reputation · 29 Mar 2025, 16:13 UTC
In Next.js (version 13.4+), the useFormStatus hook is designed to provide the pending boolean state to child components of a form during the execution of a Server Action.
While the hook functions as expected during a standard HTML form submission, there is uncertainty regarding its behavior when the Server Action is triggered programmatically. Specifically, when an action is invoked via a JavaScript event handler (such as an onClick event) rather than the native form submission process, the transition state may not propagate to the hook.
Given that Server Actions integrate with the React Transition API, it is unclear how to maintain the pending state for child components when bypassing the default form submission flow.
useFormStatus support manual invocations of Server Actions?pending state across child components when a form is not submitted natively?useFormStatus depends on a React context that the transition API sets exclusively during native form submission. When a Server Action is called from an onClick handler or other JavaScript event, that context remains unset, so the hook reports pending as false.
Likely explanation: the React Transition API does not run during manual invocation, so the pending context is never created.
Confirmed from research brief: useFormStatus reads pending state from a context set only during native form submission; manual Server Action calls do not populate this context.
useState, toggling true before the action call and false in the resolved/rejected handler.form.requestSubmit() to invoke the action through the native submission flow, which preserves the pending context that useFormStatus expects.Inspect the React DevTools pending badge on the form element; it should show pending only during native submission. Add console.log inside the Server Action to confirm it runs; if it runs but pending stays false, the context is missing. Test with form.requestSubmit() and verify the pending update.
Note: useFormStatus was introduced in React 19 / Next.js 14; earlier versions do not support the hook.
To determine which workaround best fits your case, could you confirm whether the pending state must reflect the full action lifecycle (including success) or only the in-flight period? This single detail helps narrow the recommended approach.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 29 Mar 2025, 19:55 UTC
useFormStatus only reflects the pending state when the Server Action is invoked as part of a form submission (native submit or form.requestSubmit()), because it reads the React transition state that Next.js attaches to the form’s action. Calling the action directly from an onClick handler does not start a transition, so the hook always returns pending: false. To get the same pending state for manual invocations, wrap the call in React.startTransition (or use useTransition) and lift the pending flag via useState, or simply trigger the form’s submission with form.requestSubmit().