Architecture

Modern State Management: Beyond Redux

Exploring lightweight alternatives to Redux for managing application state in 2024.

Valentin Dumas
November 15, 2023
2 min read

Redux revolutionized how we think about state management in React applications. But as the ecosystem has matured, simpler alternatives have emerged that might be better suited for many use cases.

The Problem with Redux

Don't get me wrong—Redux is powerful. But for many applications, it introduces:

  • Boilerplate overhead: Actions, reducers, selectors, middleware
  • Learning curve: Understanding the unidirectional data flow
  • Bundle size: Even with modern Redux Toolkit, it's substantial

Enter Zustand

Zustand is a small, fast, and scalable state management solution. Here's a basic example:

import { create } from 'zustand';

interface UserStore {
  user: User | null;
  setUser: (user: User) => void;
  logout: () => void;
}

const useUserStore = create<UserStore>((set) => ({
  user: null,
  setUser: (user) => set({ user }),
  logout: () => set({ user: null }),
}));

That's it. No providers, no reducers, no action types.

Using the Store

function UserProfile() {
  const user = useUserStore((state) => state.user);
  const logout = useUserStore((state) => state.logout);

  if (!user) return <LoginPrompt />;

  return (
    <div>
      <h1>{user.name}</h1>
      <button onClick={logout}>Logout</button>
    </div>
  );
}

When to Choose What

Here's my decision framework:

| Use Case | Solution | |----------|----------| | Local component state | useState | | Shared state (2-3 components) | Context API | | Complex forms | React Hook Form | | Global app state | Zustand or Jotai | | Server state | TanStack Query | | Complex state machines | XState |

Conclusion

The right tool depends on your needs. For most applications, a combination of React Query for server state and Zustand for client state provides an excellent developer experience with minimal overhead.