diff --git a/compass/app/StoreProvider.tsx b/compass/app/StoreProvider.tsx new file mode 100644 index 0000000..c8f82c1 --- /dev/null +++ b/compass/app/StoreProvider.tsx @@ -0,0 +1,17 @@ +'use client' +import { useRef } from 'react' +import { Provider } from 'react-redux' +import { makeStore, AppStore } from '../utils/store' + +export default function StoreProvider({ + children +}: { + children: React.ReactNode +}) { + const storeRef = useRef() + if (!storeRef.current) { + storeRef.current = makeStore() + } + + return {children} +} \ No newline at end of file diff --git a/compass/app/layout.tsx b/compass/app/layout.tsx index 2509089..120a901 100644 --- a/compass/app/layout.tsx +++ b/compass/app/layout.tsx @@ -1,20 +1,12 @@ import '../styles/globals.css'; -import { Metadata } from 'next' - -export const metadata: Metadata = { - title: 'Login', -} +import StoreProvider from './StoreProvider'; export default function RootLayout({ - // Layouts must accept a children prop. - // This will be populated with nested layouts or pages children, }: { children: React.ReactNode }) { return ( - - {children} - + {children} ) } \ No newline at end of file diff --git a/compass/app/store.ts b/compass/app/store.ts deleted file mode 100644 index e69de29..0000000 diff --git a/compass/features/user/userSlice.ts b/compass/utils/features/user/userSlice.ts similarity index 100% rename from compass/features/user/userSlice.ts rename to compass/utils/features/user/userSlice.ts diff --git a/compass/utils/hooks.ts b/compass/utils/hooks.ts new file mode 100644 index 0000000..da466c5 --- /dev/null +++ b/compass/utils/hooks.ts @@ -0,0 +1,8 @@ +import { useDispatch, useSelector, useStore } from 'react-redux' +import type { TypedUseSelectorHook } from 'react-redux' +import type { RootState, AppStore, AppDispatch } from './store' + + +export const useAppDispatch: () => AppDispatch = useDispatch +export const useAppSelector: TypedUseSelectorHook = useSelector +export const useAppStore: () => AppStore = useStore \ No newline at end of file diff --git a/compass/utils/store.ts b/compass/utils/store.ts new file mode 100644 index 0000000..2a9f880 --- /dev/null +++ b/compass/utils/store.ts @@ -0,0 +1,13 @@ +// store.ts +import { configureStore } from '@reduxjs/toolkit'; +import userSlice from './features/user/userSlice'; + +export const makeStore = () => configureStore({ + reducer: { + user: userSlice, + }, +}); + +export type AppStore = ReturnType; +export type RootState = ReturnType; +export type AppDispatch = AppStore['dispatch'];