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

kaz*_*iko 1 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)
Run Code Online (Sandbox Code Playgroud)

然后如何在主减速器中导入组合所有文件的减速器,我现在做的是将我的减速器的常量分割为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
Run Code Online (Sandbox Code Playgroud)

Mar*_*rda 6

我不确定你想要什么,但如果你的问题是从一个文件导出更多的值并将它们导入到另一个文件,答案是不使用export default,而是经典导出:

myreducers.js

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

然后导入它们

mainreducer.js

import { productTemplate, productAttributeValues } from "./myreducers" //fix the path
Run Code Online (Sandbox Code Playgroud)

这里export和之间的区别export default很好描述:Typescript导出与默认导出(问题与TypeScript无关).