What is optimistic UI, and how can you apply it to a form submission for a better user experience?
Short answer
Optimistic UI means immediately updating the interface as if the action already succeeded, before the server actually confirms it, using the useOptimistic hook to show the expected result right away, and then reconciling with the real server response once it arrives, which makes…
Advertisement
AModel answer
Optimistic UI means immediately updating the interface as if the action already succeeded, before the server actually confirms it, using the useOptimistic hook to show the expected result right away, and then reconciling with the real server response once it arrives, which makes the app feel instant.
'use client';
import { useOptimistic } from 'react';
function CommentList({ comments, addComment }) {
const [optimisticComments, addOptimistic] = useOptimistic(comments, (state, newComment) => [...state, newComment]);
return null; // simplified for brevity
}Real-world example
A comment section shows a new comment immediately after a user submits it, before the server has even finished saving it, making the app feel fast and responsive even on a slower network.
Likely follow-ups
- What happens if the server action fails after an optimistic update was already shown?
- How does useOptimistic differ from simply updating regular state?
Related topics