optimistic()
Show a change before the server replies.
Try the example
Save this article. Its appearance changes immediately, before the simulated server confirms. Turn on “Simulate a failed save” to see the previous state restored.
The details between an action and its result.
Article is not saved.
Add it to your project
- Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
- Connect the behavior. Copy the JavaScript tab and call its exported
mount(element)after your component’s elements exist. Replace simulated delays and data operations with your own application code. - Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.
API and lifecycle
Supply read(), write(next), request(next) and optional onError. run(next) snapshots the current UI value, applies the new value immediately, then confirms with the request. The request must reject on server failure.
- Interface
read, write, request, onError; run(next), busy- Lifecycle
- Only one mutation runs per instance; further activations return busy. On failure, the previous snapshot is written back and the result reports error. Keep snapshots immutable.
Accessibility and safety
Implementation notes
Never optimistically confirm payments, booking, admission or permissions. Server remains authoritative.
Use only for reversible, low-risk changes. Do not optimistically confirm payments, admission, bookings or permissions; the server remains authoritative.
What to check
Test a successful save, a rejected request and repeated activation while busy. Confirm rollback restores both the visual state and accessible state.