PliWould/app/store.ts
2024-06-28 14:53:48 -07:00

35 lines
1.1 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 { Product, } from "@/lib/product";
const rememberedKeys = ['products'];
const rootReducer = reducers;
export function setupStore(preloadedState = {
products: [] as Product[],
}) {
return configureStore({
reducer: rememberReducer(reducers),
preloadedState,
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(
rememberEnhancer(
AsyncStorage,
rememberedKeys,
{
persistWholeStore: true,
}
)
),
});
}
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>();