Deep Dive into State Management in ReactJS
- Introduction
- 1. State Management Basics
- 2. Setting Up the Project
- 3. State Management Options
- 4. Local State Management with
useStateanduseReducer - 5. Global State Management with Redux
- 6. Alternative Global State Management with Zustand
- More Scenarios
- 8. Some Diagrams
- Project Folder Structure
- Unit Test Cases
- Commands to Init and Add Dependencies
- Conclusion
- References
Introduction
State management is a critical aspect of building complex ReactJS applications. Proper state management ensures that your application remains scalable, maintainable, and performant. This article will deep dive into state management in ReactJS, focusing on functional components and the latest version of the tech stack. We will explore two main state management options: React Context API and Redux. Additionally, we will provide real production scenarios with TypeScript code samples, design diagrams, project folder structures, unit test cases, and commands to initialize and add dependencies using Yarn.
1. State Management Basics
1.1 What is State?
State in ReactJS is an object that represents the dynamic data of a component. It determines how the component renders and behaves. State can be local to a single component or shared across multiple components.
State management refers to managing data that changes over time, ensuring that the user interface updates appropriately. Choosing the right state management solution is crucial for developing scalable and maintainable applications. We'll explore useState and useReducer for local state management and Redux and Zustand for global state management, with detailed TypeScript examples.
1.2 Local State vs Global State
- Local State: State that is confined to a single component. It is managed using the
useStatehook. - Global State: State that is shared across multiple components. It is managed using state management libraries like React Context API or Redux.
2. Setting Up the Project
Initialize the React Project with TypeScript
Run the following commands to create a new React project with TypeScript and install the necessary dependencies:
# Create a new React app with TypeScript
yarn create react-app react-state-management --template typescript
# Navigate to the project folder
cd react-state-management
# Install Redux and Zustand for state management
yarn add redux react-redux @reduxjs/toolkit zustand
# Install TypeScript types for React and Redux
yarn add -D @types/react-redux
3. State Management Options
3.1 React Context API
The React Context API provides a way to pass data through the component tree without having to pass props down manually at every level. It is ideal for small to medium-sized applications.
import React, { createContext, useContext, useState } from 'react';
interface AuthContextType {
isAuthenticated: boolean;
login: () => void;
logout: () => void;
}
const AuthContext = createContext<AuthContextType>({
isAuthenticated: false,
login: () => {},
logout: () => {},
});
export const AuthProvider: React.FC = ({ children }) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const login = () => setIsAuthenticated(true);
const logout = () => setIsAuthenticated(false);
return (
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
{children}
</AuthContext.Provider>
);
};
export const useAuth = () => useContext(AuthContext);
4. Local State Management with useState and useReducer
4.1 Using useState
useState is simple to use and ideal for managing local state.
TypeScript Code Sample:
import React, { useState } from 'react';
const Counter: React.FC = () => {
const [count, setCount] = useState<number>(0);
const increment = (): void => {
setCount(prevCount => prevCount + 1);
};
return (
<div>
<p>Current Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
};
export default Counter;
Best Practices:
- Use
useStatefor simple and straightforward state updates. - Type your state variables for better type safety.
4.2 Using useReducer
useReducer is suitable for managing complex state logic.
TypeScript Code Sample:
import React, { useReducer } from 'react';
interface State {
count: number;
}
interface Action {
type: 'increment' | 'decrement';
}
const initialState: State = { count: 0 };
function reducer(state: State, action: Action): State {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
throw new Error('Unknown action type');
}
}
const Counter: React.FC = () => {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<p>Current Count: {state.count}</p>
<button onClick={() => dispatch({ type: 'increment' })}>Increment</button>
<button onClick={() => dispatch({ type: 'decrement' })}>Decrement</button>
</div>
);
};
export default Counter;
Best Practices:
- Use
useReducerwhen state logic is complex or involves multiple sub-values. - Type both state and actions for enhanced type safety.
5. Global State Management with Redux
Redux is a predictable state container for JavaScript apps. It is widely used for managing global state in large-scale applications.
5.1 Architecture of Redux
Redux Architecture:
+------------+ +---------------+ +-------------+
| Action | --> | Reducer | --> | Store |
+------------+ +---------------+ +-------------+
^ |
| |
+------------------------------------------+
5.2 Redux state machine
Here's a textual representation of a state machine diagram for a Redux state management flow:
+--------------------+ Action +----------------------+
| | (dispatch) | |
| React Component | -----------------> | Action |
| | | (type & payload) |
+--------------------+ +----------------------+
|
|
Dispatches action
|
v
+----------------+
| |
| Reducer |
| (pure function)|
+----------------+
|
Updates state |
based on action |
v
+----------------+
| |
| Store |
| (holds state) |
+----------------+
|
State subscription |
triggers re-render |
of component |
v
+--------------------+ Updated +----------------------+
| | State | |
| React Component | <----------------- | Store |
| | | |
+--------------------+ +----------------------+
Explanation:
- React Component: The UI component where user interactions or events trigger actions.
- Action: An object describing what type of event occurred (e.g.,
{ type: 'INCREMENT' }). - Reducer: A pure function that takes the current state and an action, then returns a new state.
- Store: The central place where the state is kept, updated, and managed.
- State Flow: Components subscribe to the store to get updates when the state changes, causing a re-render to reflect new data.
5.2 Redux Samples
The Simple One
import { createStore } from 'redux';
interface State {
count: number;
}
interface Action {
type: string;
}
const initialState: State = { count: 0 };
const reducer = (state = initialState, action: Action) => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
default:
return state;
}
};
const store = createStore(reducer);
export default store;
The Sample of Details
actions.ts:
export const increment = () => ({ type: 'INCREMENT' } as const);
export const decrement = () => ({ type: 'DECREMENT' } as const);
export type ActionType = ReturnType<typeof increment | typeof decrement>;
reducers.ts:
import { ActionType } from './actions';
interface State {
count: number;
}
const initialState: State = { count: 0 };
export const counterReducer = (state: State = initialState, action: ActionType): State => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
default:
return state;
}
};
store.ts:
import { createStore } from 'redux';
import { counterReducer } from './reducers';
const store = createStore(counterReducer);
export default store;
App.tsx:
import React from 'react';
import { Provider, useDispatch, useSelector } from 'react-redux';
import store from './state/redux/store';
import { increment, decrement } from './state/redux/actions';
interface RootState {
count: number;
}
const App: React.FC = () => {
const count = useSelector((state: RootState) => state.count);
const dispatch = useDispatch();
return (
<Provider store={store}>
<div>
<p>Current Count: {count}</p>
<button onClick={() => dispatch(increment())}>Increment</button>
<button onClick={() => dispatch(decrement())}>Decrement</button>
</div>
</Provider>
);
};
export default App;
6. Alternative Global State Management with Zustand
Zustand offers a simple and flexible state management solution without the boilerplate of Redux.
6.1 Architecture of Zustand
Simple Architecture:
Component --> useStore --> State & Actions
6.2 TypeScript Code Example
useStore.ts:
import create from 'zustand';
interface StoreState {
count: number;
increment: () => void;
decrement: () => void;
}
const useStore = create<StoreState>(set => ({
count: 0,
increment: () => set(state => ({ count: state.count + 1 })),
decrement: () => set(state => ({ count: state.count - 1 }))
}));
export default useStore;
App.tsx:
import React from 'react';
import useStore from './state/zustand/useStore';
const App
: React.FC = () => {
const { count, increment, decrement } = useStore();
return (
<div>
<p>Current Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
};
export default App;
More Scenarios
7.1 Scenario 1: User Authentication
React Context API
import React from 'react';
import { AuthProvider, useAuth } from './AuthContext';
const LoginPage: React.FC = () => {
const { login } = useAuth();
return (
<div>
<h1>Login Page</h1>
<button onClick={login}>Login</button>
</div>
);
};
const Dashboard: React.FC = () => {
const { isAuthenticated, logout } = useAuth();
return (
<div>
<h1>Dashboard</h1>
{isAuthenticated ? (
<button onClick={logout}>Logout</button>
) : (
<p>Please login to access the dashboard.</p>
)}
</div>
);
};
const App: React.FC = () => (
<AuthProvider>
<LoginPage />
<Dashboard />
</AuthProvider>
);
export default App;
Redux
import React from 'react';
import { Provider, useSelector, useDispatch } from 'react-redux';
import store from './store';
const LoginPage: React.FC = () => {
const dispatch = useDispatch();
const login = () => dispatch({ type: 'LOGIN' });
return (
<div>
<h1>Login Page</h1>
<button onClick={login}>Login</button>
</div>
);
};
const Dashboard: React.FC = () => {
const isAuthenticated = useSelector((state: any) => state.isAuthenticated);
const dispatch = useDispatch();
const logout = () => dispatch({ type: 'LOGOUT' });
return (
<div>
<h1>Dashboard</h1>
{isAuthenticated ? (
<button onClick={logout}>Logout</button>
) : (
<p>Please login to access the dashboard.</p>
)}
</div>
);
};
const App: React.FC = () => (
<Provider store={store}>
<LoginPage />
<Dashboard />
</Provider>
);
export default App;
7.2 Scenario 2: Shopping Cart
React Context API
import React, { createContext, useContext, useState } from 'react';
interface CartItem {
id: number;
name: string;
price: number;
}
interface CartContextType {
cartItems: CartItem[];
addToCart: (item: CartItem) => void;
removeFromCart: (id: number) => void;
}
const CartContext = createContext<CartContextType>({
cartItems: [],
addToCart: () => {},
removeFromCart: () => {},
});
export const CartProvider: React.FC = ({ children }) => {
const [cartItems, setCartItems] = useState<CartItem[]>([]);
const addToCart = (item: CartItem) => setCartItems([...cartItems, item]);
const removeFromCart = (id: number) =>
setCartItems(cartItems.filter((item) => item.id !== id));
return (
<CartContext.Provider value={{ cartItems, addToCart, removeFromCart }}>
{children}
</CartContext.Provider>
);
};
export const useCart = () => useContext(CartContext);
Redux
import { createStore } from 'redux';
interface CartItem {
id: number;
name: string;
price: number;
}
interface State {
cartItems: CartItem[];
}
interface Action {
type: string;
payload?: any;
}
const initialState: State = { cartItems: [] };
const reducer = (state = initialState, action: Action) => {
switch (action.type) {
case 'ADD_TO_CART':
return { cartItems: [...state.cartItems, action.payload] };
case 'REMOVE_FROM_CART':
return {
cartItems: state.cartItems.filter((item) => item.id !== action.payload),
};
default:
return state;
}
};
const store = createStore(reducer);
export default store;
8. Some Diagrams
+-------------------+ +-------------------+ +-------------------+
| App | | AuthProvider | | CartProvider |
+-------------------+ +-------------------+ +-------------------+
| | |
| (1) Auth Context | |
|--------------------------->| |
| | (2) Cart Context |
| |-------------------------->|
| | |
| (3) LoginPage | |
|<------------ --------------| |
| | |
| (4) Dashboard | |
|<------------ --------------| |
| | |
+-------------------+ +-------------------+ +-------------------+
| App | | AuthProvider | | CartProvider |
+-------------------+ +-------------------+ +-------------------+
+-------------------+ +-------------------+ +-------------------+
| User | | LoginPage | | AuthProvider |
+-------------------+ +-------------------+ +-------------------+
| | |
| (1) Click Login Button | |
|-------------------------->| |
| | (2) Dispatch Login Action |
| |-------------------------->|
| | |
| (3) Update Auth State | |
|<--------------------------| |
| | |
+-------------------+ +-------------------+ +-------------------+
| User | | LoginPage | | AuthProvider |
+-------------------+ +-------------------+ +-------------------+
Project Folder Structure
project-root/
├── src/
│ ├── components/
│ │ ├── LoginPage.tsx
│ │ ├── Dashboard.tsx
│ ├── contexts/
│ │ ├── AuthContext.tsx
│ │ ├── CartContext.tsx
│ ├── redux/
│ │ ├── store.ts
│ ├── App.tsx
│ ├── index.tsx
├── tests/
│ ├── components/
│ │ ├── LoginPage.test.tsx
│ │ ├── Dashboard.test.tsx
│ ├── contexts/
│ │ ├── AuthContext.test.tsx
│ │ ├── CartContext.test.tsx
│ ├── redux/
│ │ ├── store.test.ts
├── package.json
├── tsconfig.json
├── yarn.lock
Unit Test Cases
LoginPage.test.tsx
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { AuthProvider, useAuth } from '../../src/contexts/AuthContext';
import LoginPage from '../../src/components/LoginPage';
const TestComponent: React.FC = () => {
const { isAuthenticated } = useAuth();
return <div>{isAuthenticated ? 'Logged In' : 'Logged Out'}</div>;
};
test('LoginPage logs in user', () => {
const { getByText } = render(
<AuthProvider>
<LoginPage />
<TestComponent />
</AuthProvider>
);
fireEvent.click(getByText('Login'));
expect(getByText('Logged In')).toBeInTheDocument();
});
Dashboard.test.tsx
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { AuthProvider, useAuth } from '../../src/contexts/AuthContext';
import Dashboard from '../../src/components/Dashboard';
const TestComponent: React.FC = () => {
const { isAuthenticated } = useAuth();
return <div>{isAuthenticated ? 'Logged In' : 'Logged Out'}</div>;
};
test('Dashboard logs out user', () => {
const { getByText } = render(
<AuthProvider>
<Dashboard />
<TestComponent />
</AuthProvider>
);
fireEvent.click(getByText('Logout'));
expect(getByText('Logged Out')).toBeInTheDocument();
});
Commands to Init and Add Dependencies
# Initialize a new React project with TypeScript
yarn create react-app my-app --template typescript
# Navigate to the project directory
cd my-app
# Add Redux and React-Redux
yarn add redux react-redux
# Add React Context API (no additional packages needed)
# Add Testing Library
yarn add --dev @testing-library/react @testing-library/jest-dom
Conclusion
State management is a crucial aspect of building scalable and maintainable ReactJS applications. This article provided a deep dive into state management in ReactJS, focusing on functional components and the latest version of the tech stack. We explored two main state management options: React Context API and Redux. Additionally, we provided real production scenarios with TypeScript code samples, design diagrams, project folder structures, unit test cases, and commands to initialize and add dependencies using Yarn.
By understanding and applying these concepts, developers can effectively manage state in their ReactJS applications, ensuring they remain scalable, maintainable, and performant.
References
This article provides a comprehensive guide to state management in ReactJS, including detailed code samples, design diagrams, project folder structures, unit test cases, and commands to initialize and add dependencies using Yarn. By understanding and applying these practices, developers can leverage the full potential of state management in their ReactJS projects.