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

Practice: Optimistic UI Updates

useOptimistic(state, (current, value) => newState) returns [optimisticState, add]. Call add inside the form action: when the action ends, React goes back to the real state, so a failed message disappears by itself.

Task

The chat with the Luna base sends messages through the form action sendMessage. The link answers after 0.6 s, and a message that starts with ! pretends to hit interference and ends with an error. Fill in the blanks: ___BLANK1___ is the React 19 hook that creates an optimistic state from the real messages list and a function that appends a message, ___BLANK2___ is the function you use to show the message right away, before the base answers, and ___BLANK3___ is the list you display in the <ul>. A sent message should appear immediately with a sending note, stay without the note after the confirmation, and on an error disappear and leave a message.