Reactjs 如何持久化减速器,以便从中找到唯一的对象?
我正在为用户建立取消预定通知的能力。每个通知都有一个唯一的id,我需要找到该id才能取消通知。它们以一种称为NotificationId的状态与另外两个属性一起存储在我的提醒减速器中,以唯一标识对象 当用户尝试取消通知时,我会找到具有唯一标识符的notificationID。问题就从这里开始。有时notificationID在那里,有时不在。即使我在查找对象时在状态块中看到了要查找的对象,它仍返回未定义。状态块也会随机删除自身,并返回为零,而不是继续保持 我不确定问题是在于减速器的持久性还是我的减速器本身,我的直觉是这两者的结合 我的redux商店:Reactjs 如何持久化减速器,以便从中找到唯一的对象?,reactjs,react-native,redux,Reactjs,React Native,Redux,我正在为用户建立取消预定通知的能力。每个通知都有一个唯一的id,我需要找到该id才能取消通知。它们以一种称为NotificationId的状态与另外两个属性一起存储在我的提醒减速器中,以唯一标识对象 当用户尝试取消通知时,我会找到具有唯一标识符的notificationID。问题就从这里开始。有时notificationID在那里,有时不在。即使我在查找对象时在状态块中看到了要查找的对象,它仍返回未定义。状态块也会随机删除自身,并返回为零,而不是继续保持 我不确定问题是在于减速器的持久性还是我的
const remindersPersistConfig = {
key: 'remindersreducer',
storage: AsyncStorage,
whitelist: ['notificationIDs'],
stateReconciler: autoMergeLevel2
};
const rootPersistConfig = {
key: 'root',
storage: AsyncStorage,
whitelist: ['TodoReducer', 'RemindersReducer'],
stateReconciler: autoMergeLevel2
};
const reducers = combineReducers({
ModalReducer,
TodoReducer: persistReducer(todoPersistConfig, TodoReducer),
RemindersReducer: persistReducer(remindersPersistConfig, RemindersReducer),
AuthReducer
});
const persistedReducer = persistReducer(rootPersistConfig, reducers);
export default function storeConfiguration() {
const store = createStore(
persistedReducer,
{},
composeEnhancers(
applyMiddleware(ReduxThunk)
)
);
const persistor = persistStore(store);
return { persistor, store };
}
我的添加和取消通知操作:
export const addNotifiactionID = (item, reminderType, notificationID) => {
return {
type: ADD_NOTIFICATION_ID,
notificationID,
item,
reminderType
};
};
export const cancelNotification = (item, reminderType) => {
return {
type: CANCEL_NOTIFICATION,
id: item.id,
reminderType
};
};
负责获取信息并查找notificationID的还原程序:
const initialState = {
notificationIDs: []
};
export default (state = initialState, action) => {
switch (action.type) {
case PERSIST_REHYDRATE:
return action.payload.RemindersReducer || [];
case ADD_NOTIFICATION_ID:
return { ...state,
notificationIDs: [...state.notificationIDs,
{
itemID: action.item.id,
reminderType: action.reminderType,
notificationID: action.notificationID
}]
};
case CANCEL_NOTIFICATION: {
console.log(state.notificationIDs);
const notificationData = state.notificationIDs.find(
item => (item.itemID === action.id && item.reminderType === action.reminderType)
); //issue is redux notificationIDs randomly dissapear from reducer
console.log(notificationData);
//Notifications.cancelScheduledNotificationAsync(notificationData.notificationID);
return;
}
default:
return state;
}
};
我的控制台日志显示提醒存在,然后notificationData返回undefined(未定义),有时它会按应有的方式工作,有时NotificationID中没有任何内容,即使我没有在任何地方删除状态
任何帮助都将不胜感激:)在您的取消通知中,您似乎没有返回任何状态。可能是数据丢失的原因
case CANCEL_NOTIFICATION: {
console.log(state.notificationIDs);
const notificationData = state.notificationIDs.find(
item => (item.itemID === action.id && item.reminderType === action.reminderType)
); //issue is redux notificationIDs randomly dissapear from reducer
console.log(notificationData);
//Notifications.cancelScheduledNotificationAsync(notificationData.notificationID);
return; //<-- you are not returning a state here
}
案例取消通知:{
console.log(state.notificationIDs);
const notificationData=state.notificationIDs.find(
item=>(item.itemID==action.id&&item.rementerType===action.rementerType)
);//问题是redux NotificationId从reducer中随机消失
console.log(notificationData);
//Notifications.cancelScheduledNotificationAsync(notificationData.notificationID);
return;//我这样做了,虽然它帮助删除了数据,但并没有解决取消提醒的问题。似乎发生的情况是,当我取消通知时,在同一会话中添加的状态被删除,然后返回未定义状态。但我注意到,如果我添加通知、重新加载应用程序或nd然后取消它。会话期间的状态更新出现了一些问题,我想这就是原因。有什么想法吗?