React Dojo

Search

Search concepts, exercises and quizzes

practiceintermediate

Form with useActionState

A registration form that currently uses useState + onSubmit. Migrate the logic to useActionState to centralize state, validations, and pending in one place — without extra useState.


Objectives

  1. 1.Create an async action function that receives (prevState, formData)
  2. 2.Validate username (min 3 chars) and email (must contain @) and return { error }
  3. 3.If valid, return { ok: true, username }
  4. 4.Connect the action to useActionState and use formAction as the form's action attribute
  5. 5.Disable inputs and the button while isPending is true
your code
Hint

The action function always receives the previous state as the first argument. Return the new state — either { error } or { ok, username }. useActionState handles pending automatically.

Related concepts

Was this helpful?
Sign in to give feedback