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

perf(frontend) : Setup redux toolkit with persist #44

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
"postcss": "^8.4.38",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^9.1.2",
"@reduxjs/toolkit": "^2.2.3",
"react-router-dom": "^6.23.0",
"redux": "^5.0.1",
"redux-persist": "^6.0.0",
"tailwindcss": "^3.4.3"
},
"devDependencies": {
Expand All @@ -29,6 +33,7 @@
"@eslint/eslintrc": "^3.0.2",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@types/redux-persist": "^4.3.1",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"@vitejs/plugin-react": "^4.2.1",
Expand Down
20 changes: 14 additions & 6 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { App } from './app.tsx';

import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import { App } from './app';
import './index.scss';
import store, { persistor } from './store/store';

ReactDOM.createRoot(document.getElementById('root')!).render(
// eslint-disable-next-line react/no-deprecated
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
12 changes: 12 additions & 0 deletions src/reducers/rootReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { combineReducers } from '@reduxjs/toolkit';
import someReducer from './someReducers';

// Define RootState type based on the combined state of all reducers
export type RootState = ReturnType<typeof rootReducer>;

const rootReducer = combineReducers({
someReducer,
// Add other reducers here
});

export default rootReducer;
22 changes: 22 additions & 0 deletions src/reducers/someReducers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createSlice } from '@reduxjs/toolkit';

interface SomeState {
count: number;
}

const initialState: SomeState = {
count: 0,
};

const someSlice = createSlice({
name: 'someReducer',
initialState,
reducers: {
increment(state) {
state.count += 1;
},
},
});

export const { increment } = someSlice.actions;
export default someSlice.reducer;
20 changes: 20 additions & 0 deletions src/store/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { configureStore } from '@reduxjs/toolkit';
import { persistReducer, persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // Defaults to localStorage for web
import rootReducer from '../reducers/rootReducer.ts'; // Import your root reducer

const persistConfig = {
key: 'root',
storage,
// Add any other config options here
};

const persistedReducer = persistReducer(persistConfig, rootReducer);

const store = configureStore({
reducer: persistedReducer,
// Add any middleware or enhancers here if needed
});

export const persistor = persistStore(store);
export default store;
Loading