Next.js · Middle · Question 50 of 75
How do you return validation errors to a form from a Server Action?
- ✓Correct answer: Return an errors object and read it via
useActionState - 2Throw an exception - it appears next to the field
- 3Call
alert()on the server
Explanation
Expected errors (an invalid email) are returned as data: return { errors: { email: 'Invalid address' } }. The form renders them via the useActionState state. Exceptions are reserved for unexpected failures - error.tsx catches them.