# 1530 MINform useActionState
# 15
30 MIN
Overview
Practice creating a basic form using the useActionState approach.
Solution preview
Here's the result you'll build by completing this challenge.
Requirements
- Use the
useActionStatehook to handle form submission and state management. - Implement an
actionfunction that:- Validates that all fields (
name,email,country) are filled. - Returns an error message if any field is missing.
- Keeps the values the user already entered when validation fails.
- Waits 1 second (simulating an async request) before returning a success response.
- Resets the form fields after successful submission.
- Validates that all fields (
- Display:
- A loading state (
Submitting...) while the form is being processed. - An error message when validation fails.
- A success message when submission completes successfully.
- A loading state (
- Disable the submit button while submitting.
Notes
useActionStatereceives theactionfunction and an initial form state object.- The
actionfunction should return the updated form state after validation or submission. - Use the returned state object to show errors or success messages dynamically.
- Always use
defaultValuefor form inputs when controlled byuseActionState. - On validation error, return the submitted values so the inputs keep what the user typed; only clear them on a successful submission.
- The
actionprop on<form>should point to the function returned byuseActionState.
Tests
- shows error if fields are missing
- keeps provided fields when validation fails
- shows submitting state while waiting
- shows success message and resets fields after submit