Skip to content

Commit 33d4c3d

Browse files
committed
Add redux
1 parent 96540bf commit 33d4c3d

File tree

6 files changed

+185
-6
lines changed

6 files changed

+185
-6
lines changed

package-lock.json

+122-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"@emotion/react": "^11.10.6",
1212
"@emotion/styled": "^11.10.6",
1313
"@mui/material": "^5.11.15",
14+
"@reduxjs/toolkit": "^1.9.3",
1415
"next": "latest",
1516
"react": "18.2.0",
16-
"react-dom": "18.2.0"
17+
"react-dom": "18.2.0",
18+
"react-redux": "^8.0.5"
1719
},
1820
"devDependencies": {
1921
"@testing-library/jest-dom": "^5.16.5",

pages/_app.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import type {AppProps} from 'next/app';
22

33
import '../styles/globals.css';
44
import Layout from '../components/layout';
5+
import {store} from '../store/store';
6+
import {Provider} from 'react-redux';
57

68
export default function MyApp({Component, pageProps}: AppProps) {
79
return (
8-
<Layout>
9-
<Component {...pageProps} />
10-
</Layout>
10+
<Provider store={store}>
11+
<Layout>
12+
<Component {...pageProps} />
13+
</Layout>
14+
</Provider>
1115
);
1216
}

pages/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import Head from 'next/head';
22
import styles from '../styles/Home.module.css';
33
import Link from 'next/link';
44
import Script from 'next/script';
5+
import {useSelector, useDispatch} from 'react-redux';
6+
import {decrement, increment, selectValue} from '../store/slices/counterSlice';
57

68
export default function Home() {
9+
const count = useSelector(selectValue);
10+
const dispatch = useDispatch();
11+
712
return (
813
<div className={styles.container}>
914
<Head>
@@ -60,6 +65,9 @@ export default function Home() {
6065
</p>
6166
</a>
6267
</div>
68+
<div>The value of count is {count}</div>
69+
<button onClick={() => dispatch(increment())}>Increment</button>
70+
<button onClick={() => dispatch(decrement())}>Decrement</button>
6371
</main>
6472

6573
<footer>

store/slices/counterSlice.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {createSlice} from '@reduxjs/toolkit';
2+
import type {PayloadAction} from '@reduxjs/toolkit';
3+
import {RootState} from '../store';
4+
5+
interface CounterState {
6+
value: number;
7+
}
8+
9+
const initialState: CounterState = {
10+
value: 0,
11+
};
12+
13+
const counterSlice = createSlice({
14+
name: 'counter',
15+
initialState,
16+
reducers: {
17+
increment: (state) => {
18+
state.value += 1;
19+
},
20+
decrement: (state) => {
21+
state.value -= 1;
22+
},
23+
incrementByAmount: (state, action: PayloadAction<number>) => {
24+
state.value += action.payload;
25+
},
26+
},
27+
});
28+
29+
export const {increment, decrement, incrementByAmount} = counterSlice.actions;
30+
31+
export const selectValue = (state: RootState) => state.counter.value;
32+
33+
export default counterSlice.reducer;

store/store.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {configureStore} from '@reduxjs/toolkit';
2+
import counterReducer from './slices/counterSlice';
3+
4+
export const store = configureStore({
5+
reducer: {
6+
counter: counterReducer,
7+
},
8+
});
9+
10+
export type RootState = ReturnType<typeof store.getState>;
11+
12+
export type AppDispatch = typeof store.dispatch;

0 commit comments

Comments
 (0)