Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导航时重置Redux存储(在我重构reducer之后)_Redux_Refactoring_State_React Navigation_Reducers - Fatal编程技术网

导航时重置Redux存储(在我重构reducer之后)

导航时重置Redux存储(在我重构reducer之后),redux,refactoring,state,react-navigation,reducers,Redux,Refactoring,State,React Navigation,Reducers,我正在尝试将我的减速器重构为两个“子”减速器,并在导出到store.js之前将它们合并。但是,当我在应用程序中导航时,notificationReducer的状态将重置,而不是其他Reducer。我不确定可能是什么问题,我遵循了redux.js.org=> 关于如何重构减速器,有什么想法或建议吗 notificationReducer.js import { FETCHING_NOTIFICATION_STATUS, // NOTIFICATION FETCHING_NOTIFICA

我正在尝试将我的减速器重构为两个“子”减速器,并在导出到store.js之前将它们合并。但是,当我在应用程序中导航时,notificationReducer的状态将重置,而不是其他Reducer。我不确定可能是什么问题,我遵循了redux.js.org=>

关于如何重构减速器,有什么想法或建议吗


notificationReducer.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
你应该用它来做这些事情。因此,您的
notificationReducer
应该是三个reducer的组合

const notificationReducer = combineReducers({
  notificationStatusReducer,
  notificationDataReducer,
  markNotificationReducer 
})
希望能有所帮助