useOptimistic cho phép hiển thị state tạm thời (optimistic) ngay lập tức khi user hành động, trước khi server response.
- Nếu action thất bại, tự động rollback về state thực.
- Cú pháp:
const [optimisticMessages, addOptimistic] = useOptimistic(messages, (state, newMsg) => [...state, newMsg]). - Cải thiện perceived performance.
useOptimistic lets you immediately show a temporary optimistic state when the user takes an action, before the server responds.
- If the action fails, it automatically rolls back to the real state.
- Syntax:
const [optimisticMessages, addOptimistic] = useOptimistic(messages, (state, newMsg) => [...state, newMsg]). - This significantly improves perceived performance.