React is a JavaScript library for building user interfaces. It allows developers to create reusable UI components and manages the state of those components efficiently. Redux, on the other hand, is a state management library for JavaScript applications, which can be used with any UI framework or library. It provides a predictable state container for managing the data in an application.
Together, React and Redux form a powerful combination for building scalable, maintainable, and efficient web applications. React provides a simple and intuitive way to build UI components, while Redux provides a centralized and predictable way to manage the application state. By using these two libraries together, developers can create complex applications that are easy to reason about and maintain.
There a lot of way to setup and use redux in your reactjs project. But today i’ll share with you the way i prefer using vite, typescript and redux and why i recommend you to use that. In this tutorial, I assume that you know the basic of react, redux and kwew to use redux in reactjs project. I just focus on how we setup and organize our project’s folder structure. So let’s start.

The core components of Redux are:
In reactJs, Redux usually use when you need a global state that can be access across all components in your app. Such as the infomations about logged-in user,…etc.
yarn create vite@reduxjs/tookit which is a package that provides abstractions and utilities on top of Redux to simplify the development process and improve performance. And of course react-redux for using redux in react projectyarn add @reduxjs/toolkit react-reduxsrc
│ main.tsx
│ App.tsx
│
└───state
│ │ index,ts
│ └───user
│ │ reducer.ts
│ │ hooks.ts
│ │ index.ts
│
└───components
│ Home.tsx
│ ...
//src/user/reducer.ts
import { createSlice } from "@reduxjs/toolkit";
export type UserStateType = {
name: string;
address: string;
role: string;
}
const initialState: UserStateType = {
address: "",
name: "",
role: "",
}
const userSlice = createSlice({
initialState,
name: "user",
reducers: {
setUser: (state, action) => {
return action.payload
},
}
})
const { actions, reducer } = userSlice;
export const {
setUser
} = actions
export default reducer;// src/state/user/hook.ts
import { useDispatch, useSelector } from "react-redux"
import { AppState } from "./index"
import { useCallback } from "react"
import { UserStateType, setUser } from "./reducer";
export const useUser = () => {
const user = useSelector((state: AppState) => state.user)
const dispatch = useDispatch()
const onSetUser = useCallback((user: UserStateType) => {
dispatch(setUser(user))
}, [dispatch])
return {
user,
onSetUser
}
}
import userReducer from "./reducer"
export * from "./hook"
export default userReducerimport { configureStore } from "@reduxjs/toolkit"
import userReducer from "./user"
const rootReducer = {
user: userReducer,
//add more reducer later below
}
const store = configureStore({
reducer: rootReducer,
devTools: true,
})
export default store
export type AppState = ReturnType<typeof store.getState>import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { Provider } from "react-redux";
import store from "./state";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement)
.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);import React from "react";
import { useUser } from "./state/user/hook";
function App() {
const { user, onSetUser } = useUser();
const updateUser = () => {
onSetUser({
name: "John Doe",
address: "123 Main St",
role: "admin",
});
};
return (
<div>
<h1>React Redux TS</h1>
<h2>current user state in redux: {JSON.stringify(user)}</h2>
<br />
<button onClick={updateUser}>Update User</button>
</div>
);
}
export default App;