终极版商店导航时重置(后我重构减速)

问题描述 投票:2回答:1

我想我的减速器重构为两个“子”减速并出口到store.js之前将它们结合起来。然而,当我在我的应用我浏览我的notificationReducer的状态就会reseted,而不是其他减速。我不确定可能是什么问题,我已经跟着指南(排序)从redux.js.org => Separating Data Handling by Domain

如何你重构减速任何想法或建议吗?


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
redux refactoring state react-navigation reducers
1个回答
1
投票

您应该使用combineReducers这样的事情。所以,你的notificationReducer应该是你的3轮减速机的组合。

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

希望这将有助于

© www.soinside.com 2019 - 2024. All rights reserved.