Skip to content

Commit 47a1318

Browse files
committed
feat: add Redux store setup with persistence and custom hooks for dispatch and selector
1 parent b31ffcc commit 47a1318

4 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/hooks/useAppDispatch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { useDispatch } from "react-redux";
2+
import type { AppDispatch } from "../store";
3+
4+
export const useAppDispatch = () => useDispatch<AppDispatch>();

src/hooks/useAppSelector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { TypedUseSelectorHook, useSelector } from "react-redux";
2+
import type { RootState } from "../store";
3+
4+
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

src/store/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import AsyncStorage from "@react-native-async-storage/async-storage";
2+
import { combineReducers, configureStore } from "@reduxjs/toolkit";
3+
import {
4+
FLUSH,
5+
PAUSE,
6+
PERSIST,
7+
persistReducer,
8+
persistStore,
9+
PURGE,
10+
REGISTER,
11+
REHYDRATE,
12+
} from "redux-persist";
13+
14+
import browserReducer from "../features/browser/browserSlice";
15+
import scriptsReducer from "../features/scripts/scriptsSlice";
16+
import settingsReducer from "../features/settings/settingsSlice";
17+
18+
const persistConfig = {
19+
key: "root",
20+
storage: AsyncStorage,
21+
whitelist: ["scripts", "settings"],
22+
};
23+
24+
const rootReducer = combineReducers({
25+
browser: browserReducer,
26+
scripts: scriptsReducer,
27+
settings: settingsReducer,
28+
});
29+
30+
const persistedReducer = persistReducer(persistConfig, rootReducer);
31+
32+
export const store = configureStore({
33+
reducer: persistedReducer,
34+
middleware: (getDefaultMiddleware) =>
35+
getDefaultMiddleware({
36+
serializableCheck: {
37+
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
38+
},
39+
}),
40+
devTools: __DEV__, // Enable Redux DevTools only in development
41+
});
42+
43+
export const persistor = persistStore(store);
44+
45+
export type RootState = ReturnType<typeof store.getState>;
46+
export type AppDispatch = typeof store.dispatch;

src/types/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { BrowserState } from "../features/browser/types";
2+
import { ScriptsState } from "../features/scripts/types";
3+
import { SettingsState } from "../features/settings/types";
4+
5+
export interface RootState {
6+
browser: BrowserState;
7+
scripts: ScriptsState;
8+
settings: SettingsState;
9+
}

0 commit comments

Comments
 (0)