A comprehensive React application designed to help you learn Redux state management through practical implementation. This project demonstrates modern Redux patterns, async operations, and best practices in a real-world application.
This Contact Manager is specifically designed as a Redux learning resource that covers:
- β Redux Fundamentals - Actions, Reducers, Store, and Dispatch
- β Async Operations - Redux Thunk for API calls
- β Modern Redux Patterns - Loading states, error handling, and optimistic updates
- β Real-world Scenarios - CRUD operations with proper state management
- β Best Practices - Clean code structure and separation of concerns
# Clone and install
git clone https://github.com/devmahmud/ContactManager.git
cd ContactManager
npm install
# Start learning
npm run dev
// src/store.js - Redux store configuration
{
contact: {
contacts: [], // All contacts
contact: {}, // Single contact for editing
loading: false, // Loading state for async operations
error: null // Error handling
}
}
// src/actions/contactActions.js
export const getContacts = () => async (dispatch) => {
dispatch({ type: 'GET_CONTACTS_START' }); // Loading starts
try {
const response = await axios.get('/users');
dispatch({ type: 'GET_CONTACTS_SUCCESS', payload: response.data });
} catch (error) {
dispatch({ type: 'GET_CONTACTS_ERROR', payload: error.message });
}
};
// src/reducers/contactReducer.js
export default function (state = initialState, action) {
switch (action.type) {
case 'GET_CONTACTS_START':
return { ...state, loading: true, error: null };
case 'GET_CONTACTS_SUCCESS':
return { ...state, contacts: action.payload, loading: false };
case 'GET_CONTACTS_ERROR':
return { ...state, loading: false, error: action.payload };
default:
return state;
}
}
// Using Redux in React components
import { useSelector, useDispatch } from 'react-redux';
function Contacts() {
const dispatch = useDispatch();
const { contacts, loading, error } = useSelector(state => state.contact);
useEffect(() => {
dispatch(getContacts()); // Dispatch action
}, [dispatch]);
// Component renders based on Redux state
}
Technology | Purpose | Learning Value |
---|---|---|
Redux 5.0.1 | State Management | Core Redux concepts and patterns |
Redux Thunk | Async Operations | Handling API calls and side effects |
React 18.3.1 | UI Framework | Modern React with hooks |
React Router v6 | Navigation | Client-side routing |
Vite | Build Tool | Modern development experience |
src/
βββ store.js # Redux store configuration
βββ actions/ # Action creators (async operations)
β βββ contactActions.js # CRUD operations with Redux Thunk
βββ reducers/ # State reducers
β βββ contactReducer.js # Contact state management
β βββ index.js # Root reducer
βββ components/ # React components using Redux
βββ contacts/ # Contact CRUD components
βββ layout/ # Shared components
// Actions dispatch loading states
dispatch({ type: 'GET_CONTACTS_START' });
// Components show loading UI
{loading && <LoadingSpinner />}
// Reducers handle errors
case 'GET_CONTACTS_ERROR':
return { ...state, loading: false, error: action.payload };
// Components display errors
{error && <div className="alert alert-danger">{error}</div>}
// Immediate UI updates with rollback on error
case 'ADD_CONTACT_SUCCESS':
return { ...state, contacts: [action.payload, ...state.contacts] };
// Redux Thunk for async actions
export const addContact = (contact) => async (dispatch) => {
dispatch({ type: 'ADD_CONTACT_START' });
try {
const response = await axios.post('/users', contact);
dispatch({ type: 'ADD_CONTACT_SUCCESS', payload: response.data });
} catch (error) {
dispatch({ type: 'ADD_CONTACT_ERROR', payload: error.message });
}
};
- β Create - Add new contacts with Redux state updates
- β Read - Fetch and display contacts from Redux store
- β Update - Edit contacts with optimistic updates
- β Delete - Remove contacts with confirmation modal
- β Loading States - Show spinners during async operations
- β Error Handling - Display errors from Redux state
- β Form Management - Controlled components with Redux
- β Navigation - Route-based state management
- β Redux Thunk - Async action creators
- β useSelector/useDispatch - Modern React-Redux hooks
- β Immutable Updates - Proper state immutability
- β Action Types - Consistent action naming
npm run dev # Start development server
npm run build # Build for production
npm run preview # Preview production build
npm run deploy # Deploy to GitHub Pages
The application is configured for automatic deployment to GitHub Pages:
npm run deploy
Live Demo: https://devmahmud.github.io/ContactManager
- β Automatic Build - Builds before deployment
- β Client-side Routing - 404.html handles React Router
- β Asset Optimization - Proper base path configuration
- β Jekyll Bypass - .nojekyll file prevents Jekyll processing
Uses JSONPlaceholder API for realistic data:
- GET
/users
- Fetch all contacts - POST
/users
- Create new contact - PUT
/users/:id
- Update contact - DELETE
/users/:id
- Delete contact
- Store - Single source of truth
- Actions - Plain objects describing what happened
- Reducers - Pure functions that specify how state changes
- Dispatch - Method to trigger state changes
- Selectors - Functions to extract data from state
- Redux Thunk - Middleware for async operations
- Loading States - Managing async operation states
- Error Boundaries - Handling and displaying errors
- Optimistic Updates - Immediate UI feedback
- State Normalization - Efficient data structure
After studying this project, you'll understand:
- β How to structure Redux applications
- β How to handle async operations with Redux Thunk
- β How to manage loading and error states
- β How to integrate Redux with React components
- β How to implement CRUD operations with Redux
- β How to write clean, maintainable Redux code
This is a learning project! Feel free to:
- Add new Redux patterns
- Improve error handling
- Add more complex state management scenarios
- Create additional learning examples
MIT License - Feel free to use this for learning and teaching Redux!
Start your Redux journey today! π
This project is designed to be a comprehensive learning resource for Redux. Each component and pattern is implemented with educational value in mind.