Skip to content

Commit 8576150

Browse files
redux-setup with a counter (#25)
1 parent 3d9dfe9 commit 8576150

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

package-lock.json

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
},
1212
"dependencies": {
1313
"@radix-ui/react-slot": "1.1.0",
14+
"@reduxjs/toolkit": "2.2.6",
1415
"class-variance-authority": "0.7.0",
1516
"clsx": "2.1.1",
1617
"lucide-react": "0.399.0",
1718
"react": "^18.3.1",
1819
"react-dom": "^18.3.1",
20+
"react-redux": "9.1.2",
1921
"react-router-dom": "6.24.0",
2022
"tailwind-merge": "2.3.0",
2123
"tailwindcss-animate": "1.0.7"

src/Pages/LandingPage.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
import { Button } from "@/components/ui/button";
22
import { Link } from "react-router-dom";
3+
import { increment, decrement } from "../store/features/counter/counterSlice";
4+
import { useDispatch, useSelector } from "react-redux";
5+
import { RootState } from "../store/store";
36

47
const LandingPage = () => {
8+
const count = useSelector((state: RootState) => state.counter.count);
9+
const dispatch = useDispatch();
10+
511
return (
612
<div className="h-[100vh] flex flex-col justify-center items-center">
713
<h1 className="text-[128px] font-medium-">LibraryHub</h1>
814
<p className="text-[36px] font-medium">
915
Your central place for all library needs
1016
</p>
17+
18+
19+
<div className="flex mt-[20px] gap-[10px]">
20+
<Button
21+
onClick={() => dispatch(decrement())}
22+
variant="outline"
23+
24+
>
25+
-
26+
</Button>
27+
<div className="border p-[7px] rounded-[7px]">{count}</div>
28+
<Button
29+
onClick={() => dispatch(increment())}
30+
variant="outline"
31+
32+
>
33+
+
34+
</Button>
35+
36+
</div>
37+
38+
1139
<div className="flex gap-[20px] mt-[50px]">
1240
<Link to={"management/login"}>
1341
<Button variant="outline">Create A Library</Button>

src/main.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import React from 'react'
22
import ReactDOM from 'react-dom/client'
33
import App from './App.tsx'
44
import './index.css'
5+
import store from './store/store'
6+
import { Provider } from 'react-redux'
7+
58

69
ReactDOM.createRoot(document.getElementById('root')!).render(
710
<React.StrictMode>
11+
<Provider store={store}>
812
<App />
13+
</Provider>
914
</React.StrictMode>,
1015
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createSlice } from "@reduxjs/toolkit";
2+
3+
const initialState={
4+
count:0
5+
}
6+
7+
export const counterSlice=createSlice({
8+
name:'counter',
9+
initialState,
10+
reducers:{
11+
increment:(state)=>{
12+
state.count+=1;
13+
},
14+
decrement:(state)=>{
15+
state.count-=1;
16+
}
17+
18+
}
19+
20+
});
21+
22+
export const {increment,decrement} = counterSlice.actions;
23+
export default counterSlice.reducer;

src/store/store.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// store.ts
2+
import { configureStore } from '@reduxjs/toolkit';
3+
import counterReducer from './features/counter/counterSlice'; // Adjust the import path if necessary
4+
5+
const store = configureStore({
6+
reducer: {
7+
counter: counterReducer,
8+
},
9+
});
10+
11+
export type RootState = ReturnType<typeof store.getState>;
12+
export type AppDispatch = typeof store.dispatch;
13+
14+
export default store;

0 commit comments

Comments
 (0)