Overview
The SSEGH Security Cameras application uses Redux Toolkit (RTK) for centralized state management. Redux Toolkit simplifies Redux configuration and provides powerful utilities for managing camera data and filters.Store Configuration
The Redux store is configured instore/store.js using Redux Toolkit’s configureStore:
store/store.js
Redux Toolkit’s
configureStore automatically sets up the Redux DevTools extension and includes middleware like Redux Thunk.Provider Setup
The store is provided to the React application through the Provider component:app.js:32-39
State Structure
The application maintains a single state slice calledcamaras containing an array of camera objects:
Camaras Slice
ThecamarasSlice manages all camera-related state using Redux Toolkit’s createSlice:
store/camarasSlice.js:151-191
Redux Toolkit’s
createSlice automatically generates action creators and action types based on the reducer names.Initial State
The initial state contains hardcoded camera data:store/camarasSlice.js:3-148
Reducers
The slice defines five reducer functions that handle different state operations:filterCamara
Filters the camera array by ID:store/camarasSlice.js:155-158
filterModelo
Filters cameras by model name, used for displaying individual camera details:store/camarasSlice.js:159-162
components/viewcamera.js:50-52
filterCamarasAside
Implements complex multi-filter logic for sidebar filters:store/camarasSlice.js:163-182
Multiple Filters
When multiple filters are applied, return only cameras that match ALL filter criteria using
Array.every().backToInitialState
Resets the state to the original camera list:store/camarasSlice.js:187-189
components/main.js:6-8
/productos page always displays all cameras when first loaded.
returnPreviousState
Calls another reducer to reset state:store/camarasSlice.js:183-186
Redux Toolkit’s
caseReducers property allows one reducer to call another reducer function.Accessing State in Components
Components access Redux state using theuseSelector hook from React-Redux:
Basic Selection
components/main.js:10
camaras array from the Redux store.
Selecting Specific Data
components/viewcamera.js:48
Component Integration
app.js:3-12
Dispatching Actions
Components dispatch actions using theuseDispatch hook:
Getting Dispatch Function
components/main.js:4
Dispatching in Effects
components/main.js:6-8
Dispatching with Payloads
Pass data to actions via the payload:components/navbarAside.js:20
State Flow
The application follows Redux’s unidirectional data flow:Complete Flow Example
Here’s how filtering works from start to finish:Action Dispatched
The component dispatches the
filterCamarasAside action with the filter value as payload.Reducer Executes
The reducer filters the camera array based on the payload:
store/camarasSlice.js:163-182
Components Re-render
All components using
useSelector((state) => state.camaras) receive the updated state and re-render.Redux Toolkit Utilities
The application uses several Redux Toolkit utilities:createSlice
Simplifies reducer logic with automatic action creator generation:store/camarasSlice.js:1
current
Provides a readable snapshot of draft state:store/camarasSlice.js:168
current, you’d get a Proxy object instead of the actual camera data.
configureStore
Sets up the store with good defaults:store/store.js:3-7
- Redux Thunk middleware for async operations
- Redux DevTools Extension integration
- Immutability and serializability checks in development
Redux DevTools Integration
Redux Toolkit automatically enables Redux DevTools, allowing you to inspect state changes, time-travel debug, and replay actions.
- Install the Redux DevTools Extension in your browser
- Open the application
- Open browser DevTools and navigate to the Redux tab
- Inspect actions, state diffs, and time-travel through state changes
Common Patterns
Reset State on Mount
components/main.js:6-8
Filter and Navigate
components/navbarAside.js:19-22
Extract Then Filter
components/viewcamera.js:44-52
Best Practices
Keep State Minimal
Store only essential data in Redux. Derive computed values in components or selectors.
Use Redux Toolkit
Redux Toolkit simplifies Redux with less boilerplate and built-in best practices.
Dispatch in Effects
Use
useEffect to dispatch actions on mount or when dependencies change.Select Efficiently
Use
useSelector with specific selectors to prevent unnecessary re-renders.Next Steps
Architecture
Learn about the overall application structure
Routing
Understand how React Router manages navigation