Performance: Why optimistic UI improves perceived performance
Optimistic UI improves how fast users feel the app responds by showing changes immediately before server confirmation.
function handleSubmit(data) {
setState({ updated: true }); // optimistic update
fetch('/api/update', { method: 'POST', body: JSON.stringify(data) })
.catch(() => setState({ updated: false }));
}async function handleSubmit(data) { await fetch('/api/update', { method: 'POST', body: JSON.stringify(data) }); setState({ updated: true }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Waiting for server response before UI update | 1 update after response | 1 reflow per update | Medium paint cost | [X] Bad |
| Optimistic UI update before server response | 1 immediate update | 1 reflow immediately | Medium paint cost | [✓] Good |
useFetcher?fetcher.submit(data); updateUIImmediately(); calls submit then updates UI immediately. await fetcher.submit(data); updateUIImmediately(); waits (await) before UI update, not optimistic.const fetcher = useFetcher();
function handleClick() {
fetcher.submit({ text: 'Hello' });
setMessage('Sending...');
}const fetcher = useFetcher();
function handleSubmit(data) {
fetcher.submit(data);
setItems([...items, data]);
}
// Later, if server rejects, no rollback is donePass the test and this pattern moves unknown