Overview
Learn advanced Redux patterns for handling asynchronous operations, side effects, and complex state updates. Master Redux Thunks for API calls, optimistic updates, and advanced data flow patterns.Redux Thunks
Handle async logic in Redux
Side Effects
Manage API calls and data fetching
Action Creators
Create complex action logic
Error Handling
Handle loading and error states
The Challenge: Async Logic
Reducers must be pure, synchronous functions. But real apps need to:Common Async Operations
Common Async Operations
- Fetch data from APIs
- Save data to backend
- Wait for user input
- Perform calculations with delays
- Handle WebSocket messages
Where to Put Async Code?
Where to Put Async Code?
Two main approaches:
- Inside Components - useEffect with fetch
- Inside Action Creators - Redux Thunks
Redux Thunk Basics
A thunk is a function that returns another function, allowing delayed or async logic.- Regular Action Creator
- Thunk Action Creator
How Thunks Work:
- You dispatch a thunk (function)
- Redux middleware intercepts it
- Middleware calls the function with
dispatchandgetState - Function performs async work
- Function dispatches regular actions when ready
Complete Example: Shopping Cart
Here’s a real implementation from the course showing async data fetching and saving.Cart Slice
cart-slice.js
Async Action Creators (Thunks)
cart-actions.js
Using Thunks in Components
App.js
Thunk Patterns
- Basic Thunk
- Async Thunk
- Thunk with getState
- Thunk with Parameters
Error Handling
Handle errors gracefully in thunks:Error Handling Best Practices:
- Always catch errors in thunks
- Dispatch error actions to update UI
- Include helpful error messages
- Log errors for debugging
- Show user-friendly messages
Loading States and UI Feedback
Manage loading and notification states:ui-slice.js
Notification Component
Notification.js
Optimistic Updates
Update UI immediately, then sync with backend:Redux Thunk vs useEffect
- useEffect Approach
- Redux Thunk Approach
- Simple for component-specific data
- Easy to understand
- Logic tied to component
- Hard to reuse
- Difficult to test
Avoiding Infinite Loops
Prevent sending data on initial load:Advanced Patterns
Chaining Thunks
Chaining Thunks
Conditional Dispatching
Conditional Dispatching
Debouncing API Calls
Debouncing API Calls
Canceling Requests
Canceling Requests
Testing Thunks
Best Practices
Keep Thunks Focused
Each thunk should handle one async operation
Always Handle Errors
Use try-catch and dispatch error actions
Show Loading States
Give users feedback during async operations
Avoid Infinite Loops
Be careful with useEffect dependencies
Extract API Calls
Separate API logic into service functions
Type Your Actions
Use TypeScript or constants for action types
Alternative: RTK Query
For complex data fetching, consider RTK Query:RTK Query provides:
- Automatic caching
- Automatic refetching
- Loading and error states
- Optimistic updates
- Request deduplication
Resources
Redux Thunk Docs
Official Redux Thunk documentation
RTK Query
Powerful data fetching solution