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
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;
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;
4. Add Slice to Store
import counterReducer from "./slices/counterSlice";
export const store = configureStore({
reducer: {
counter: counterReducer
}
});
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>
);
}
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>
);
}
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
dispatchandselector
β 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)