Close the earlier lessons and build this from the brief. The starter's form saves cards, but shows nothing while saving, saves blank titles, and loses failures: try This will fail and see what happens.
The brief
Rebuild the form with a form action:
useActionStateholds{ error }. A blank title returns Enter a title. without calling the server. A refused card returns "This will fail" wasn't saved: The server refused the card.- While saving, the button is disabled and says Saving….
useOptimisticshows each new card at once, faded, with Saving…, until the server answers. A refused card disappears.- Under the form, 3 cards saved counts only cards the server confirmed.
Acceptance checks:
- Add with a blank title shows the error and sends nothing.
- Adding Quick one shows it immediately as saving, then as saved, and the count goes from 2 to 3.
- This will fail appears, disappears, and leaves the error. The count doesn't change.
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Hint 1: one action, two hooks
The function you pass to useActionState is the action, so call
addOptimisticCard inside it, before the await.
Hint 2: the count
Count cards, the real state, not optimisticCards.
Show one solution
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- Why doesn't
memohelp when a parent passes a new arrow function each render? - What does
useDeferredValuelet React do with a slow render? - Why don't optimistic updates need rollback code when saving fails?
Completion is saved in this browser only. It does not measure mastery.