DEV Community

Priyansh0510
Priyansh0510

Posted on

Advanced State Management Patterns in React with TypeScript 🧠

As React applications grow in complexity, managing state becomes increasingly challenging. TypeScript adds an extra layer of type safety, helping catch errors early and improve developer experience. In this comprehensive guide, we'll explore advanced state management patterns in React, leveraging TypeScript to create robust, scalable, and maintainable solutions.

The Evolution of State Management in React

React's state management has evolved significantly since its inception. From class components with this.setState to functional components with hooks, and now with advanced patterns and external libraries, the ecosystem offers numerous ways to handle state. Today, we'll focus on modern, TypeScript-enhanced approaches.

1. Advanced Custom Hooks

Custom hooks are a powerful way to encapsulate and reuse stateful logic. Let's create a sophisticated custom hook that manages pagination with TypeScript:

import { useState, useCallback, useMemo } from 'react';

interface UsePaginationProps<T> {
  data: T[];
  itemsPerPage: number;
}

interface UsePaginationReturn<T> {
  currentPage: number;
  totalPages: number;
  currentData: T[];
  nextPage: () => void;
  prevPage: () => void;
  goToPage: (page: number) => void;
}

function usePagination<T>({ data, itemsPerPage }: UsePaginationProps<T>): UsePaginationReturn<T> {
  const [currentPage, setCurrentPage] = useState(1);

  const totalPages = useMemo(() => Math.ceil(data.length / itemsPerPage), [data.length, itemsPerPage]);

  const currentData = useMemo(() => {
    const start = (currentPage - 1) * itemsPerPage;
    return data.slice(start, start + itemsPerPage);
  }, [data, currentPage, itemsPerPage]);

  const nextPage = useCallback(() => {
    setCurrentPage((page) => Math.min(page + 1, totalPages));
  }, [totalPages]);

  const prevPage = useCallback(() => {
    setCurrentPage((page) => Math.max(page - 1, 1));
  }, []);

  const goToPage = useCallback((page: number) => {
    const pageNumber = Math.max(1, Math.min(page, totalPages));
    setCurrentPage(pageNumber);
  }, [totalPages]);

  return {
    currentPage,
    totalPages,
    currentData,
    nextPage,
    prevPage,
    goToPage,
  };
}

export default usePagination;
Enter fullscreen mode Exit fullscreen mode

This hook provides a type-safe way to handle pagination in your components:

interface Item {
  id: number;
  name: string;
}

const YourComponent: React.FC = () => {
  const items: Item[] = [/* ... */];
  const { currentData, nextPage, prevPage, currentPage, totalPages } = usePagination({
    data: items,
    itemsPerPage: 10
  });

  return (
    <div>
      {currentData.map(item => <div key={item.id}>{item.name}</div>)}
      <button onClick={prevPage} disabled={currentPage === 1}>Previous</button>
      <button onClick={nextPage} disabled={currentPage === totalPages}>Next</button>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

2. Context with TypeScript: Advanced Patterns

While React Context is powerful, it can lead to unnecessary re-renders. Let's create an optimized, type-safe context setup:

import React, { createContext, useContext, useReducer, useMemo, useCallback } from 'react';

// Define the state shape
interface AppState {
  user: User | null;
  theme: 'light' | 'dark';
  notifications: Notification[];
}

// Define action types
type Action =
  | { type: 'SET_USER'; payload: User | null }
  | { type: 'SET_THEME'; payload: 'light' | 'dark' }
  | { type: 'ADD_NOTIFICATION'; payload: Notification }
  | { type: 'REMOVE_NOTIFICATION'; payload: string };

// Create separate contexts for state and dispatch
const StateContext = createContext<AppState | undefined>(undefined);
const DispatchContext = createContext<React.Dispatch<Action> | undefined>(undefined);

// Reducer function
function appReducer(state: AppState, action: Action): AppState {
  switch (action.type) {
    case 'SET_USER':
      return { ...state, user: action.payload };
    case 'SET_THEME':
      return { ...state, theme: action.payload };
    case 'ADD_NOTIFICATION':
      return { ...state, notifications: [...state.notifications, action.payload] };
    case 'REMOVE_NOTIFICATION':
      return {
        ...state,
        notifications: state.notifications.filter(n => n.id !== action.payload)
      };
    default:
      return state;
  }
}

// Provider component
export const AppProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
  const [state, dispatch] = useReducer(appReducer, {
    user: null,
    theme: 'light',
    notifications: []
  });

  return (
    <StateContext.Provider value={state}>
      <DispatchContext.Provider value={dispatch}>
        {children}
      </DispatchContext.Provider>
    </StateContext.Provider>
  );
};

// Custom hooks for using the context
export function useAppState() {
  const context = useContext(StateContext);
  if (context === undefined) {
    throw new Error('useAppState must be used within an AppProvider');
  }
  return context;
}

export function useAppDispatch() {
  const context = useContext(DispatchContext);
  if (context === undefined) {
    throw new Error('useAppDispatch must be used within an AppProvider');
  }
  return context;
}

// Action creators
export function useActions() {
  const dispatch = useAppDispatch();

  return useMemo(() => ({
    setUser: useCallback((user: User | null) => 
      dispatch({ type: 'SET_USER', payload: user }), [dispatch]),
    setTheme: useCallback((theme: 'light' | 'dark') => 
      dispatch({ type: 'SET_THEME', payload: theme }), [dispatch]),
    addNotification: useCallback((notification: Notification) => 
      dispatch({ type: 'ADD_NOTIFICATION', payload: notification }), [dispatch]),
    removeNotification: useCallback((id: string) => 
      dispatch({ type: 'REMOVE_NOTIFICATION', payload: id }), [dispatch]),
  }), [dispatch]);
}
Enter fullscreen mode Exit fullscreen mode

This setup provides several benefits:

  • Separation of state and dispatch contexts to prevent unnecessary re-renders
  • Type-safe action creators and state updates
  • Memoized action creators to prevent unnecessary re-creations

Usage in components:

const UserProfile: React.FC = () => {
  const { user, theme } = useAppState();
  const { setTheme } = useActions();

  return (
    <div>
      <h1>Welcome, {user?.name}</h1>
      <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
        Toggle Theme
      </button>
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

3. Integrating External Libraries with TypeScript

While custom solutions are powerful, sometimes external libraries like Redux or MobX are necessary for larger applications. Let's look at how to integrate Redux with TypeScript for type-safe global state management:

First, install the necessary packages:

npm install @reduxjs/toolkit react-redux
Enter fullscreen mode Exit fullscreen mode

Now, let's set up our store with TypeScript:

import { configureStore, createSlice, PayloadAction } from '@reduxjs/toolkit';

// Define the state shape
interface RootState {
  counter: number;
  user: User | null;
}

// Create a slice
const counterSlice = createSlice({
  name: 'counter',
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
    decrement: (state) => state - 1,
    incrementByAmount: (state, action: PayloadAction<number>) => state + action.payload,
  },
});

const userSlice = createSlice({
  name: 'user',
  initialState: null as User | null,
  reducers: {
    setUser: (state, action: PayloadAction<User | null>) => action.payload,
  },
});

// Configure the store
const store = configureStore({
  reducer: {
    counter: counterSlice.reducer,
    user: userSlice.reducer,
  },
});

// Export types and action creators
export type AppDispatch = typeof store.dispatch;
export const { increment, decrement, incrementByAmount } = counterSlice.actions;
export const { setUser } = userSlice.actions;

// Create typed hooks
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

export default store;
Enter fullscreen mode Exit fullscreen mode

Using the store in a component:

import React from 'react';
import { useAppSelector, useAppDispatch, increment, setUser } from './store';

const Counter: React.FC = () => {
  const count = useAppSelector(state => state.counter);
  const user = useAppSelector(state => state.user);
  const dispatch = useAppDispatch();

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => dispatch(increment())}>Increment</button>
      <button onClick={() => dispatch(setUser({ id: '1', name: 'John Doe' }))}>Set User</button>
      {user && <p>Welcome, {user.name}</p>}
    </div>
  );
};

export default Counter;
Enter fullscreen mode Exit fullscreen mode

This setup provides full type safety for your Redux store, actions, and selectors.

Conclusion

Advanced state management in React with TypeScript offers powerful tools for building complex, maintainable applications. By leveraging custom hooks, optimized context patterns, and integrating external libraries with TypeScript, we can create robust state management solutions that are both flexible and type-safe.

Remember these key points:

  • Use custom hooks to encapsulate complex stateful logic
  • Optimize context to prevent unnecessary re-renders
  • Leverage TypeScript for type-safe action creators and state updates
  • Consider external libraries for large-scale applications, but always with TypeScript integration

As you build more complex React applications, these patterns will help you manage state effectively while maintaining code quality and developer productivity. Happy coding!

Top comments (0)