Introduction
In this blog post, we will explore how to use Redux with Next.js and create todo and disptach it to Redux store and and pass the todo (state) to a Page and display the created Todo. We will cover the process of setting up the necessary Redux store and cover the end to end flow.
Create Next.js application
- npx create-next-app@latest
Install dependencies
- npm install --save @reduxjs/toolkit
- npm install --save react-redux
- npm install --save redux-persist
Folders and files needed
- Create folder lib under src folder(src/lib)
- Create Interface Todos under lib folder (lib/Todos.ts)
export interface ITodo {
todosState: [Todo]
}
export interface Todo {
todoName: string,
description: string,
userName: string,
id: string
}
export interface ITodo {
import { ITodo, Todo } from "@/lib/Todo";
import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
const initialState: ITodo = {
todosState: [{
todoName: '',
description: '',
userName: '',
id: ''
}]
};
export const todosSlice = createSlice({
name: "todos",
initialState,
reducers: {
setTodosState: (state, action: PayloadAction) => {
state.todosState.push(action.payload);
},
},
});
export const { setTodosState } = todosSlice.actions;
export const todosReducer = todosSlice.reducer;
import { configureStore } from "@reduxjs/toolkit";
import { useDispatch, TypedUseSelectorHook, useSelector } from "react-redux";
import { todosReducer } from "./store/todosSlice";
export const store = configureStore({
reducer: { todos: todosReducer },
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({ serializableCheck: false }),
});
export type RootState = ReturnType;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch();
export const useAppSelector: TypedUseSelectorHook = useSelector
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
import { todosReducer } from "./todosSlice";
const todosPersistConfig = {
key: "todos",
storage: storage,
whitelist: ["todosState"],
};
const rootReducer = combineReducers({
todos: persistReducer(todosPersistConfig, todosReducer),
});
export const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({ serializableCheck: false }),
});
Now start writing Component side code
- Create StoreProvider.tsx file under app folder(app/StoreProvider.tsx) and add the below code to StoreProvider.tsx file
- Create todos.tsx file under app folder(app/todos.tsx) and add the below code to todos.tsx file
- Create todos-updater.tsx file under app folder(app/todos-updater.tsx) and add the below code to todos-updater.tsx file
- Add the below code to page.tsx file
Now start the application with below command in Project root folder
- npm run dev
Conclusion
In this blog post, we learned how to create Redux store for Todos to hold the data and passed the created todos to todos-updater to display the created todo in simple todos application.
Comment on this Post if you have any questions.
Made with VideoToBlog






Comments
Post a Comment