Working with Redux in Next App
Last Updated :
30 Jan, 2024
Integrating Redux into a Next app streamlines complex state management. This tutorial outlines steps to seamlessly integrate Redux with Next, demonstrating the creation of a basic counter with increment and decrement buttons, with Redux managing its state.
Approach to integrate Redux in Next Application:
As Next is built on React, we’ll utilize the ‘redux‘ and ‘react-redux‘ node packages. Our process involves creating a Redux store, defining actions and reducers, wrapping our app’s base with ‘Provider‘, and utilizing ‘useSelector‘ and ‘useDispatch‘ hooks to interact with the Redux data layer.
Steps to Set Up the App:
Step 1: Open the terminal and create a NextJS app using the following command:
npx create-next-app next-redux
Step 2: Switching into the project directory:
cd next-redux
Step 3: Install ‘redux’ and ‘react-redux’ and ‘@reduxjs/toolkit’ package by running the following command:
npm install redux react-redux @reduxjs/toolkit
Project Structure:
Project Structure
The updated dependencies in package.json file will look like:
"dependencies": {
"@reduxjs/toolkit": "^2.1.0",
"next": "14.1.0",
"react": "^18",
"react-dom": "^18",
"react-redux": "^9.1.0",
"redux": "^5.0.1"
}
Step 4: In this step we will setup our redux store. Create a folder named ‘redux’ at the root of your project. Further create a file ‘store.js’ inside it.
Step 5: Update ‘store.js’ to include the counterReducer.
Step 6: Wrap the base of the application pages/_app.js with Provider from react-redux.
Step 7: Make the use of state the we just created by opening pages/index.js and writing the following logic. The explanation of each and every line is written in comments.
Explanation:
- Initial state for the counter is set to 40 as the default value.
- The ‘@reduxjs/toolkit’ library’s ‘createSlice’ function is utilized to establish a slice.
- Within reducers, ‘increment’ and ‘decrement’ functions are defined to respectively increase or decrease the counter value.
- Action creators (increment, decrement) are destructured and exported, along with the reducer function created by ‘createSlice’.
Example: Below is the code example for the complete working of the redux:
Javascript
import Image from "next/image" ;
import { Inter } from "next/font/google" ;
import {
useDispatch,
useSelector
} from "react-redux" ;
import {
decrement,
increment
} from "@/redux/counterReducer" ;
const inter = Inter({ subsets: [ "latin" ] });
export default function Home() {
const counter =
useSelector((state) => state.counter);
const dispatch = useDispatch();
const handleDecrease = () => {
dispatch({
type: decrement,
});
};
const handleIncrease = () => {
dispatch({
type: increment,
});
};
return (
<div className= "mx-auto w-[400px] h-[400px]
flex items-center space-x-10" >
{ }
<h1 className= "min-w-36 text-xl tracking-wider" >
Counter with Redux + Next
</h1>
{ }
<button onClick={handleDecrease}
className= "bg-red-600 text-white p-4" >
DECREASE
</button>
{ }
<h1>{counter.value}</h1>
{ }
<button onClick={handleIncrease}
className= "bg-green-600 text-white p-4" >
INCREASE
</button>
</div>
);
}
|
Javascript
import "@/styles/globals.css" ;
import { store } from "../redux/store" ;
import { Provider } from "react-redux" ;
export default
function App(
{
Component,
pageProps
}) {
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}
|
Javascript
import { createSlice }
from "@reduxjs/toolkit" ;
const initialState = {
value: 40,
};
export const counterSlice =
createSlice({
name: "counter" ,
initialState,
reducers: {
increment: (state) => {
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload;
},
},
});
export const {
increment,
decrement,
incrementByAmount
} = counterSlice.actions;
export default counterSlice.reducer;
|
Javascript
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from './counterReducer'
export const store = configureStore({
reducer: {
counter: counterReducer,
},
})
|
Step to run the app:
npm run dev
Output: Navigate to http://localhost:3000/
Share your thoughts in the comments
Please Login to comment...