PliWould/app/store.ts

39 lines
1.3 KiB
TypeScript

import { useDispatch, useSelector } from 'react-redux';
import { configureStore } from '@reduxjs/toolkit';
import { rememberReducer, rememberEnhancer } from 'redux-remember';
import reducers from "@/features/product/productSlice"
import AsyncStorage from '@react-native-async-storage/async-storage';
import { ProductData, } from "@/lib/product";
import {Length} from "convert"
const rememberedKeys = ['products'];
const rootReducer = reducers;
const isBrowser = (typeof window !== "undefined");
export function setupStore(preloadedState = {
products: [] as ProductData[],
units: "ft" as Length,
}) {
return configureStore({
reducer: rememberReducer(reducers),
preloadedState,
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(
rememberEnhancer(
isBrowser ? window.localStorage : AsyncStorage,
rememberedKeys,
{
persistWholeStore: false,
},
)
),
});
}
export type RootState = ReturnType<typeof rootReducer>;
export type AppStore = ReturnType<typeof setupStore>;
export type AppDispatch = AppStore['dispatch'];
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();