#15form useActionState
Overview
Practice creating a basic form using the useActionState approach.
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.
- 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. - The
actionprop on<form>should point to the function returned byuseActionState.
Tests
- shows error if fields are missing
- shows submitting state while waiting
- shows success message and resets fields after submit