I'm implementing "Add Favorite" and store it those items in async-storage Vs react-redux. It's working fine when after adding and switching from Home Tab to Favorite Tab. But I got the error "date.toDate is not a function" after killing app and go to Favorite Tab.
Any suggestions and solutions? Thank you so much!
Component
<CardInfo
key={data.id.toString()}
promotionToDate={moment(data.expireDate.toDate()).format('lll')}
/>
Action
export const getPromotionsAction = () => async dispatch => {
dispatch({type: GET_PROMOTION});
const response = await firebase.firestore().collection('xxxx').get();
const values = [];
response.docs.forEach(res => {
values.push(res.data());
});
dispatch({type: GET_PROMOTION, payload: values});
return values;
};
export const addFavoriteAction = fav => dispatch => {
dispatch({
type: ADD_PROMOTION_TO_FAVORITE_LIST,
payload: fav,
});
};
Store
// import rootReducer from '../redux';
import thunk from 'redux-thunk';
import PromotionReducer from './reducers';
import {persistStore, persistReducer} from 'redux-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {createStore, combineReducers, applyMiddleware} from 'redux';
import {logger} from 'redux-logger';
const persistConfig = {
key: 'root',
storage: AsyncStorage,
whitelist: ['setPromotionToFavorites'],
};
const rootReducer = combineReducers({
PromotionReducers: persistReducer(persistConfig, PromotionReducer),
});
export const store = createStore(rootReducer, applyMiddleware(thunk, logger));
export const persistor = persistStore(store);
export type RootStateType = ReturnType<typeof store.getState>;
NOTED I'm using Firestore, Momentjs enter image description here
CodePudding user response:
The functions and objects are reference types in JavaScript, Nearly everything in JavaScript is an object other than six things that are not objects: null, undefined, strings, numbers, boolean, and symbols.
And when we look at the AsyncStorage, If we have to store the values in AsyncStorage then we have to parse the data in the string datatype.
At that time JSON.stringify is used which converts the objects into a string, and the functions cannot be converted into strings.
and We use redux-persist with AsyncStorage which stores the objects into it by JSON.stringify.
In Conslusion, We have to make sure to convert the functions into JSON Objects by returning the values from them.