DEV Community

Cover image for How to Set Up Redux in Next.js with TypeScript (The Easy Way πŸš€)
Muhammad Hamid Raza
Muhammad Hamid Raza

Posted on

How to Set Up Redux in Next.js with TypeScript (The Easy Way πŸš€)

Ever felt your Next.js app growing faster than your ability to manage state?

One component needs data from another, props are drilling like crazy, and suddenly your code feels like a messy WhatsApp group chat πŸ˜….

That’s where Redux with Next.js and TypeScript comes in.

In this guide, I’ll show you how to set up Redux in Next.js with TypeScript step by step, using simple language, real-life examples, and developer-friendly tips.

Think of it like unlocking your phone β€” once you know the password, everything just works πŸ“±.


What Is Redux?

Redux is a state management library.

In simple words:

πŸ‘‰ Redux is a central store where your app’s data lives.

Real-world example

Imagine a water tank on your roof:

  • Every room (component) uses water
  • You don’t store water separately in every room
  • Everyone pulls water from one main tank

Redux works the same way:

  • One store
  • Many components
  • Clean and predictable data flow

Why Redux Matters in Next.js

Next.js apps grow fast β€” especially dashboards, eCommerce stores, or SaaS apps.

Without Redux:

  • Props drilling everywhere
  • Hard-to-debug state issues
  • Repeated API calls

With Redux:

  • One source of truth
  • Better scalability
  • Cleaner and maintainable code

Ask yourself πŸ€”:

Do I want to manage state calmly or debug chaos at 2 AM?


Benefits of Using Redux with TypeScript

Real-life benefits developers actually care about:

  • βœ… Type safety – Fewer runtime errors
  • βœ… Predictable state – Easy debugging
  • βœ… Scalable architecture – Perfect for large apps
  • βœ… Great DevTools – Time-travel debugging is πŸ”₯
  • βœ… Team-friendly – Everyone follows the same pattern

Redux Toolkit vs Traditional Redux

Redux Toolkit Traditional Redux
Less boilerplate Too much code
Easy setup Complex setup
Built-in best practices Manual configuration
TypeScript friendly Type definitions everywhere 😡

πŸ‘‰ Redux Toolkit wins for modern Next.js apps.


Step-by-Step: Set Up Redux in Next.js with TypeScript

1. Install Required Packages

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

Easy. No headache. No extra config.


2. Create the Redux Store

Create a folder: store/index.ts

import { configureStore } from "@reduxjs/toolkit";

export const store = configureStore({
  reducer: {}
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
Enter fullscreen mode Exit fullscreen mode

3. Create a Slice (Example: Counter)

store/slices/counterSlice.ts

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

interface CounterState {
  value: number;
}

const initialState: CounterState = {
  value: 0
};

const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1;
    },
    decrement: (state) => {
      state.value -= 1;
    },
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload;
    }
  }
});

export const { increment, decrement, incrementByAmount } = counterSlice.actions;
export default counterSlice.reducer;
Enter fullscreen mode Exit fullscreen mode

4. Add Slice to Store

import counterReducer from "./slices/counterSlice";

export const store = configureStore({
  reducer: {
    counter: counterReducer
  }
});
Enter fullscreen mode Exit fullscreen mode

5. Wrap Next.js App with Provider

For Next.js App Router (app/layout.tsx):

"use client";
import { Provider } from "react-redux";
import { store } from "../store";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <Provider store={store}>
      {children}
    </Provider>
  );
}
Enter fullscreen mode Exit fullscreen mode

Boom πŸ’₯ Redux is live.


6. Use Redux in a Component

"use client";
import { useDispatch, useSelector } from "react-redux";
import { RootState } from "../store";
import { increment, decrement } from "../store/slices/counterSlice";

export default function Counter() {
  const count = useSelector((state: RootState) => state.counter.value);
  const dispatch = useDispatch();

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => dispatch(increment())}>+</button>
      <button onClick={() => dispatch(decrement())}>-</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Simple. Clean. Powerful.


Best Tips: Do’s & Don’ts

βœ… Do’s

  • Use Redux Toolkit
  • Keep slices small and focused
  • Use TypeScript types properly
  • Use custom hooks for dispatch and selector

❌ Don’ts

  • Don’t store everything in Redux
  • Don’t mutate state outside slices
  • Don’t overcomplicate early

Common Mistakes Developers Make

  • ❌ Using Redux for simple local state
  • ❌ Forgetting "use client" in App Router
  • ❌ Mixing server and client logic
  • ❌ Not typing state properly

Avoid these, and you’re already ahead of many devs πŸ˜‰.


Final Thoughts + Call to Action

Setting up Redux in Next.js with TypeScript is not scary anymore β€” and now you know it.

If you enjoyed this guide and want more real-world developer blogs, tutorials, and tips:

πŸ‘‰ Visit https://hamidrazadev.com

πŸ‘‰ Read. Learn. Build better apps.

Because clean code = peaceful mind 🧠✨

Happy coding! πŸš€

Top comments (0)