Coding task · JavaScript and React course · Module 9: Modern React Hooks

Practice: Form Validation with useActionState

Validation in the action: every error goes into the errors object under the field name, e.g. errors.name. By returning values together with errors you keep what the user typed, because React resets the form after the action.

Task

App.jsx contains a crew member registration form (fields name, role and experience) and an action registerCrewMember, which checks the data on the "server side" and returns an errors object with a message for every invalid field. Fill in the blanks: ___BLANK1___ is the condition that is true when the name values.name has fewer than 2 characters, ___BLANK2___ is the object with the entered values that the action returns together with the errors, and ___BLANK3___ is the key in state.errors holding the message for the role field. React clears the form fields when the action finishes, so defaultValue restores them from the state. Invalid data should show a separate message next to every invalid field and keep the entered values, valid data a greeting with the name.