useActionData for Action Responses
What you'll learn: How to access validation errors or success messages returned from your route actions in your components.
When you submit a form using React Router's Form component (which triggers a route action), that action can return data—like validation errors, success messages, or partial form data. The useActionData hook lets you access whatever your action returned, so you can display feedback to the user without managing extra state.
How It Works
After an action runs, React Router stores its return value in memory. useActionData retrieves that value for the current route. If the action returns an object with errors, you can display them. If it returns a success message, you can show a banner. The hook returns undefined before the first action submission, and updates with the latest action response afterward.
// In your route action
export async function action({ request }) {
const formData = await request.formData();
const email = formData.get('email');
if (!email.includes('@')) {
return { error: 'Invalid email address' };
}
// Save to database...
return { success: true, message: 'Profile updated!' };
}
// In your component
function ProfileForm() {
const actionData = useActionData();
return (
<Form method="post">
{actionData?.error && (
<div className="error">{actionData.error}</div>
)}
{actionData?.success && (
<div className="success">{actionData.message}</div>
)}
<input name="email" type="email" />
<button type="submit">Save</button>
</Form>
);
}
The beauty of this pattern is that you're building on top of standard forms—the action validation happens server-side (or in your route), and React Router handles the plumbing to surface that feedback.
Key Takeaway: useActionData gives you access to whatever your action returns, enabling clean validation feedback and success messages without manual state management.