在一个reducer文件REDUX中导出多个变量

时间:2017-02-20 07:47:49

标签: reactjs redux

我想问一下如何在redux

中导出1个文件reducer中的多个const

myreducers.js

import * as ActionTypes from '../actions/secone'

// PRODUCT TEMPLATE STATE
const productTemplate = (state={records:[], isFetching: false}, action) => {
switch (action.type) {
    case ActionTypes.PRODUCT_TEMPLATE_REQUEST:
        return { ...state, isFetching: true}
    case ActionTypes.PRODUCT_TEMPLATE_SUCCESS:
        return {
            records: action.response,
            isFetching: false,
        }
    default:
        return state;
}
}

// PRODUCT ATTRIBUTE VALUES STATE
const productAttributeValues = (state={records:[], isFetching: false}, action) => {
switch (action.type) {
    case ActionTypes.PRODUCT_ATTRIBUTE_VALUES_REQUEST:
        return { ...state, isFetching: true}
    case ActionTypes.PRODUCT_ATTRIBUTE_VALUES_SUCCESS:
        return {
            records: action.response,
            isFetching: false,
        }
    default:
        return state;
}
}

export default (productTemplate, productAttributeValues)

然后如何在主减速器中导入组合所有文件的减速器, 我现在所做的是将我的Reducer的const const分成1个文件,这样效率不高,

mainreducer.js

import { combineReducers } from 'redux'
import * as ActionTypes from '../actions/auth'
import authentication from './auth'
import productBrand from './secone'
import productTemplate from './product'
import resCity from './resCity'
import { routerReducer } from 'react-router-redux'

// Updates error message to notify about the failed fetches.
const errorMessage = (state = null, action) => {
const { type, error } = action

if (type === ActionTypes.RESET_ERROR_MESSAGE) {
    return null
} else if (error) {
    return action.error
}
  return state
}

const rootReducer = combineReducers({
    authentication,
    errorMessage,
    productTemplate,
    // productAttributeValues,
    productBrand,
    resCity,
    routing: routerReducer
})

export default rootReducer

1 个答案:

答案 0 :(得分:6)

我不确定您想要实现的目标,但如果您的问题是从一个文件中导出更多值并将其导入另一个文件,那么答案不是使用export default,而是使用经典导出:

myreducers.js

export const productTemplate = (state={records:[], isFetching: false}, action) => { ... }
export const productAttributeValues = (state={records:[], isFetching: false}, action) => { ... }

然后导入它们

mainreducer.js

import { productTemplate, productAttributeValues } from "./myreducers" //fix the path

exportexport default之间的区别在这里有很好的描述:Typescript export vs. default export(问题与TypeScript无关)。