This project is built to help understand the fundamentals of Redux Toolkit in a React application. It includes basic features like user authentication and a counter, showing how to manage global state effectively with modern Redux practices.
β
How to set up Redux Toolkit
β
Creating slices with actions and reducers
β
Connecting store to React using <Provider>
β
Reading state with useSelector
β
Dispatching actions with useDispatch
β
Structuring app logic cleanly into components
The app basically checks if a user is "logged in" (mocked).
If yes β shows profile + counter.
If no β shows login screen.
npm install @reduxjs/toolkit react-redux
import { createSlice } from "@reduxjs/toolkit";
const slice = createSlice({
name: "yourSliceName",
initialState: {},
reducers: {
yourAction(state, action) {
// update state here
},
},
});
export const yourActions = slice.actions;
export default slice.reducer;
import { configureStore } from "@reduxjs/toolkit";
import yourReducer from "./yourSlice";
const store = configureStore({
reducer: { yourKey: yourReducer },
});
export default store;
import { Provider } from "react-redux";
import store from "./store";
<Provider store={store}>
<App />
</Provider>
import { useSelector, useDispatch } from "react-redux";
import { yourActions } from "./yourSlice";
const Component = () => {
const data = useSelector(state => state.yourKey.someData);
const dispatch = useDispatch();
return (
<>
<button onClick={() => dispatch(yourActions.yourAction())}>Click</button>
</>
);
};