导航时重置 Redux 存储(在我重构化简器之后)



我正在尝试将我的化简器重构为两个"子"化简器,并在导出到存储之前将它们组合在一起.js。但是,当我在我的应用程序中导航时,我的通知缩减器的状态被重置,而不是其他化简器。我不确定可能是什么问题,我已经按照 redux.js.org => 中的指南(有点) 按域分离数据处理

关于如何重构化简器的任何想法或提示?


通知化简器.js

import {
FETCHING_NOTIFICATION_STATUS, // NOTIFICATION
FETCHING_NOTIFICATION_STATUS_SUCCESS,
FETCHING_NOTIFICATION_STATUS_FAILURE,
FETCHING_NOTIFICATION_DATA, // NOTIFICATION
FETCHING_NOTIFICATION_DATA_SUCCESS,
FETCHING_NOTIFICATION_DATA_FAILURE,
FETCHING_MARK_NOTIFICATION_AS_UNSEEN, // NOTIFICATION
FETCHING_MARK_NOTIFICATION_AS_UNSEEN_SUCCESS,
FETCHING_MARK_NOTIFICATION_AS_UNSEEN_FAILURE
} from '../Actions/actionTypes'
const fetchingData = {
isFetching: false,
dataFetched: false,
error: false,
errorMsg: '',
}
const initialState = {
notificationStatus: {
...fetchingData,
hasNotifications: false,
},
notificationData: {
...fetchingData,
data: [],
}, 
markNotification: {
...fetchingData,
isUnseen: false,
}, 
}
const { notificationStatus, notificationData, markNotification } = initialState
const notificationStatusReducer = (state = notificationStatus, action) => {
switch(action.type) {
case FETCHING_NOTIFICATION_STATUS:
return {
...state,
isFetching: true,
}
case FETCHING_NOTIFICATION_STATUS_SUCCESS:
return {
...state,
isFetching: false,
dataFetched: true,
hasNotifications: action.data,
}
case FETCHING_NOTIFICATION_STATUS_FAILURE:
return {
...state,
isFetching: false,
error: true,
errorMsg: action.errorMsg,
}
default:
return state
}
}
const notificationDataReducer = (state = notificationData, action) => {
switch(action.type) {
case FETCHING_NOTIFICATION_DATA:
return {
...state,
isFetching: true
}
case FETCHING_NOTIFICATION_DATA_SUCCESS:
return {
...state,
isFetching: false,
dataFetched: true,
data: action.data,
}
case FETCHING_NOTIFICATION_DATA_FAILURE:
return {
...state,
isFetching: false,
error: true,
errorMsg: action.errorMsg,
}
default:
return state
}
}
const markNotificationReducer = (state = markNotification, action) => {
switch(action.type) {
case FETCHING_MARK_NOTIFICATION_AS_UNSEEN:
return {
...state,
isFetching: true
}
case FETCHING_MARK_NOTIFICATION_AS_UNSEEN_SUCCESS:
return {
...state,
isFetching: false,
dataFetched: true,
isUnseen: true,
}
case FETCHING_MARK_NOTIFICATION_AS_UNSEEN_FAILURE:
return {
...state,
isFetching: false,
error: true,
errorMsg: action.errorMsg,
}
default:
return state
}
}
const notificationReducer = (state = initialState, action) => {
return {
notificationStatusReducer : notificationStatusReducer(state.notificationStatus, action),
notificationDataReducer : notificationDataReducer(state.notificationStatus, action),
markNotificationReducer : markNotificationReducer(state.markNotification, action),
}
}
export default notificationReducer

你应该用combineReducers来做这些事情。所以你的notificationReducer应该是你的三个减速器的组合。

const notificationReducer = combineReducers({
notificationStatusReducer,
notificationDataReducer,
markNotificationReducer 
})

希望会有所帮助

最新更新