Reactjs 在一个REDUX文件中导出多个变量
我想问一下如何在redux中导出多个const-in-1文件缩减器 myreducers.jsReactjs 在一个REDUX文件中导出多个变量,reactjs,redux,Reactjs,Redux,我想问一下如何在redux中导出多个const-in-1文件缩减器 myreducers.js import * as ActionTypes from '../actions/secone' // PRODUCT TEMPLATE STATE const productTemplate = (state={records:[], isFetching: false}, action) => { switch (action.type) { case ActionTypes.PR
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)
export const productTemplate = (state={records:[], isFetching: false}, action) => { ... }
export const productAttributeValues = (state={records:[], isFetching: false}, action) => { ... }
然后如何在主减速器中导入合并所有文件的减速器,
我现在所做的是将我的减缩器的常量拆分成一个文件,这是没有效率的
main.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
import { productTemplate, productAttributeValues } from "./myreducers" //fix the path
我不确定您想要实现什么,但如果您的问题是从一个文件导出更多值并将其导入另一个文件,那么答案不是使用
导出默认值,而是使用经典导出:
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)
export const productTemplate = (state={records:[], isFetching: false}, action) => { ... }
export const productAttributeValues = (state={records:[], isFetching: false}, action) => { ... }
然后导入它们
main.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
import { productTemplate, productAttributeValues } from "./myreducers" //fix the path
<>代码>出口和<代码>导出默认这里描述得很好:(问题不在于打印稿)。