Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(frontend): setup redux for state management #70

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
},
"dependencies": {
"@radix-ui/react-slot": "^1.0.2",
"@reduxjs/toolkit": "^2.2.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"lucide-react": "^0.334.0",
"next": "14.1.0",
"react": "^18",
"react-dom": "^18",
"react-redux": "^9.1.0",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7"
},
Expand Down
18 changes: 18 additions & 0 deletions packages/frontend/src/app/StoreProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use client';
import { useRef } from 'react';
import { Provider } from 'react-redux';
import { store, AppStore } from '@/lib/store';

export default function StoreProvider({
children
}: {
children: React.ReactNode;
}) {
const storeRef = useRef<AppStore>();
if (!storeRef.current) {
// Create the store instance the first time this renders
storeRef.current = store();
}

return <Provider store={storeRef.current}>{children}</Provider>;
}
5 changes: 4 additions & 1 deletion packages/frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import StoreProvider from './StoreProvider';
import '@/styles/globals.css';

const inter = Inter({ subsets: ['latin'] });
Expand All @@ -16,7 +17,9 @@ export default function RootLayout({
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<body className={inter.className}>
<StoreProvider>{children}</StoreProvider>
</body>
</html>
);
}
8 changes: 8 additions & 0 deletions packages/frontend/src/lib/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useDispatch, useSelector, useStore } from 'react-redux';
import type { TypedUseSelectorHook } from 'react-redux';
import type { RootState, AppDispatch, AppStore } from './store';

// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppDispatch: () => AppDispatch = useDispatch;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
export const useAppStore: () => AppStore = useStore;
11 changes: 11 additions & 0 deletions packages/frontend/src/lib/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { configureStore } from '@reduxjs/toolkit';

export const store = () => {
RickyC0626 marked this conversation as resolved.
Show resolved Hide resolved
return configureStore({
CeeJayyy007 marked this conversation as resolved.
Show resolved Hide resolved
reducer: {}
});
};

export type AppStore = ReturnType<typeof store>;
export type RootState = ReturnType<AppStore['getState']>;
export type AppDispatch = AppStore['dispatch'];