A practical demonstration of classic async updates vs optimistic updates in React with Redux Toolkit.
This project was inspired by TanStack Query's optimistic updates. While TanStack Query provides an excellent implementation of optimistic updates along with many other powerful features, not every application needs a full-featured data management library. This demo shows how to implement optimistic updates using just Redux Toolkit, making it a lighter alternative for smaller applications that only need this specific feature.
This project demonstrates the difference between traditional asynchronous updates and optimistic updates in a web application. It helps developers understand when and how to implement optimistic updates to enhance user experience.
In traditional async updates, the UI waits for server confirmation before reflecting changes:
// Classic Async Flow
const handleDelete = async (id) => {
setLoading(true); // 1. Show loading state
try {
await api.delete(id); // 2. Wait for server
updateUI(id); // 3. Update UI after success
} catch (error) {
showError(error); // 4. Show error if failed
}
setLoading(false); // 5. Hide loading state
};
With optimistic updates, we update the UI immediately and handle failures gracefully:
// Optimistic Update Flow
const handleDelete = async (id) => {
const previousState = [...items]; // 1. Store current state
updateUI(id); // 2. Update UI immediately
try {
await api.delete(id); // 3. Make API call in background
} catch (error) {
revertUI(previousState); // 4. Revert on failure
showError(error);
}
};
- CRUD operations with both update patterns
- Real-world API simulation with delays
- Error handling and state reversion
- Clean and modular React components
- TypeScript for type safety
- Redux Toolkit for state management
- Modern UI with dark/light mode support
- React 18
- Redux Toolkit
- TypeScript
- Vite
- React Toastify
- UUID
-
Clone the repository:
git clone https://github.com/ZtheLeader/redux-optimistic.git
-
Install dependencies:
cd redux-optimistic npm install
-
Run the development server:
npm run dev
-
Open http://localhost:5173 in your browser
The demo includes a simple article management interface where you can:
- Create new articles
- Update article titles
- Delete articles
Each operation has two buttons:
- Regular button: Uses classic async update pattern
- Optimistic button: Uses optimistic update pattern
Try both to see the difference in user experience!
Optimistic updates are best suited for:
- High-confidence operations (likely to succeed)
- Actions that need to feel snappy
- Non-critical data updates
- Good network conditions
Avoid for:
- Critical financial transactions
- Data-dependent operations
- Operations with complex rollback requirements
- High-risk operations
src/
├── components/ # React components
├── store/ # Redux store and slices
├── hooks/ # Custom React hooks
├── types/ # TypeScript types
├── utils/ # Utility functions
└── api/ # API simulation
Feel free to open issues and pull requests!
MIT
Muhammad Zain - @ZtheLeader