Skip to content

Commit 3c942ca

Browse files
committed
chore: redux
1 parent 84ebe1b commit 3c942ca

File tree

6 files changed

+144
-5
lines changed

6 files changed

+144
-5
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"@reduxjs/toolkit": "^2.2.1",
1213
"next": "14.1.0",
1314
"react": "^18",
1415
"react-dom": "^18",
16+
"react-redux": "^9.1.0",
17+
"redux": "^5.0.1",
1518
"sass": "^1.71.1",
1619
"styled-components": "^6.1.8"
1720
},

src/app/layout.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata } from "next";
22
import { Inter } from "next/font/google";
33
import "./globals.css";
44
import StyledComponentsRegistry from "@/lib/registry";
5+
import StoreProvider from "@/lib/StoreProvider";
56

67
const inter = Inter({ subsets: ["latin"] });
78

@@ -18,7 +19,9 @@ export default function RootLayout({
1819
return (
1920
<html lang="ko">
2021
<body className={inter.className}>
21-
<StyledComponentsRegistry>{children}</StyledComponentsRegistry>
22+
<StoreProvider>
23+
<StyledComponentsRegistry>{children}</StyledComponentsRegistry>
24+
</StoreProvider>
2225
</body>
2326
</html>
2427
);

src/lib/StoreProvider.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use client";
2+
import { useRef } from "react";
3+
import { Provider } from "react-redux";
4+
import { makeStore, AppStore } from "../lib/store";
5+
6+
export default function StoreProvider({
7+
children,
8+
}: {
9+
children: React.ReactNode;
10+
}) {
11+
const storeRef = useRef<AppStore>();
12+
if (!storeRef.current) {
13+
// Create the store instance the first time this renders
14+
storeRef.current = makeStore();
15+
16+
// 만약, 초기 데이터를 초기화해야한다면 이 부분에 작성한다.
17+
}
18+
19+
return <Provider store={storeRef.current}>{children}</Provider>;
20+
}

src/lib/hooks.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useDispatch, useSelector, useStore } from "react-redux";
2+
import type { TypedUseSelectorHook } from "react-redux";
3+
import type { RootState, AppDispatch, AppStore } from "./store";
4+
5+
// Use throughout your app instead of plain `useDispatch` and `useSelector`
6+
export const useAppDispatch: () => AppDispatch = useDispatch;
7+
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
8+
export const useAppStore: () => AppStore = useStore;

src/lib/store.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { configureStore } from "@reduxjs/toolkit";
2+
3+
export const makeStore = () => {
4+
return configureStore({
5+
reducer: {},
6+
});
7+
};
8+
9+
// Infer the type of makeStore
10+
export type AppStore = ReturnType<typeof makeStore>;
11+
// Infer the `RootState` and `AppDispatch` types from the store itself
12+
export type RootState = ReturnType<AppStore["getState"]>;
13+
export type AppDispatch = AppStore["dispatch"];

0 commit comments

Comments
 (0)