This repository contains a comprehensive implementation of Redux and Redux Toolkit in a React application. Visit my portfolio at devashu.tech for more projects.
- React
- Redux & Redux Toolkit
- TypeScript
- Vite
- Store: The single source of truth that holds the entire state tree of your application
- State: The current data in your Redux store
- Action: A plain JavaScript object that describes what happened
- Reducer: A pure function that takes the current state and an action, and returns a new state
- Dispatch: The method used to send actions to the store
- Subscribe: The method used to listen for changes to the store
- Action Creator: A function that creates and returns an action object
- Middleware: Functions that intercept actions before they reach the reducer
- Selector: Functions that extract specific pieces of state from the store
- Slice: A collection of Redux reducer logic and actions for a single feature
- Immutability: The principle that state should not be modified directly
- Time Travel: The ability to move back and forth through the state history
- createSlice: A function that automatically generates action creators and action types
- createAsyncThunk: A function that handles async operations
- configureStore: A function that sets up the Redux store with good defaults
- createEntityAdapter: A utility for managing normalized state
- createSelector: A function for creating memoized selectors
- Normalized State: A flattened state structure that avoids data duplication
- Derived State: State that is computed from other state values
- Side Effects: Operations that affect the outside world (API calls, etc.)
- Thunk: A function that wraps an expression to delay its evaluation
- Saga: A middleware that uses generator functions to handle side effects
-
What is Redux, and why is it used in React applications?
- Redux is a predictable state container for JavaScript applications
- It helps manage global application state in a predictable way
- Useful for large applications with complex state management needs
- Provides a single source of truth for application state
-
Core principles of Redux (Actions, Reducers, Store)
- Single Source of Truth: One main store holds all application state
- State is Read-Only: State can only be changed by dispatching actions
- Changes are made with Pure Functions: Reducers are pure functions that specify state changes
-
What is a Redux Store?
- Central state container that holds the application's global state
- Different from React component state which is local and component-specific
- Provides methods like getState(), dispatch(), and subscribe()
-
Data Flow in Redux
- Unidirectional data flow
- Action → Dispatcher → Reducer → Store → UI
- Makes state changes predictable and traceable
-
Importance of Immutability in Redux
- Ensures predictable state updates
- Enables features like time-travel debugging
- Helps with performance optimization through change detection
- Achieved using methods like spread operator, Object.assign(), or Immer
-
Redux Actions and Action Creators
- Actions: Plain objects describing what happened
- Action Creators: Functions that create actions
- Must have a 'type' property
- Can contain additional data in the payload
-
Role of Reducers
- Pure functions that specify how state changes
- Take current state and action as arguments
- Return new state without modifying the original
- Handle different action types
-
Redux Selectors
- Functions that extract specific pieces of state
- Enable data transformation and memoization
- Help with performance optimization
- Can compute derived data
-
Redux Thunk vs Redux Saga
- Redux Thunk:
- Simpler, good for basic async actions
- Can dispatch multiple actions
- Works with promises
- Redux Saga:
- More powerful, uses generators
- Better for complex async flows
- Easier to test
- Redux Thunk:
-
Redux Middleware
- Provides extension point between dispatching action and reducer
- Common examples: redux-thunk, redux-logger, redux-saga
- Can modify actions, dispatch multiple actions, handle async operations
-
Main Features of Redux Toolkit
- Simplified store setup
- Includes useful utilities
- Built-in Immer for immutable updates
- Automatic action creators with createSlice
-
Slices in Redux Toolkit
- Combines reducers, actions, and action creators
- Automatically generates action creators
- Simplifies reducer logic
- Promotes code organization
-
RTK vs Traditional Redux
- Less boilerplate code
- Built-in best practices
- Better TypeScript support
- Simplified async logic
-
Benefits of createSlice()
- Automatic action creator generation
- Simplified reducer logic
- Built-in Immer support
- Type-safe actions
-
State Normalization
- Flatten state structure
- Avoid nested data
- Use IDs for references
- Easier updates and lookups
-
Time-travel Debugging
- Track state changes over time
- Replay actions
- Debug state issues
- Available through Redux DevTools
-
Memoization in Selectors
- Prevent unnecessary recalculations
- Use createSelector from Reselect
- Cache selector results
- Improve performance
-
Redux in Small vs Large Applications
- Consider app size and complexity
- Evaluate state management needs
- Consider alternatives for small apps
- Balance complexity vs benefits
-
Clone the repository: ```bash git clone https://github.com/Ashukr321/Complete-React-Redux-State-Management.git ```
-
Install dependencies: ```bash npm install ```
-
Run the development server: ```bash npm run dev ```
Feel free to contribute to this project by creating issues or submitting pull requests.
- Ashu Kumar
- Portfolio: devashu.tech
This project is licensed under the MIT License.