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)