在一个 reducer 文件 REDUX 中导出多个变量
Posted
技术标签:
【中文标题】在一个 reducer 文件 REDUX 中导出多个变量【英文标题】:export multiple variable in one reducer file REDUX 【发布时间】:2017-07-09 09:46:13 【问题描述】:我想问一下redux中如何在1个文件reducers中导出多个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)
然后如何在组合所有文件的主减速器中导入减速器, 我现在所做的是将我的减速器的 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】:我不确定你想要达到什么,但如果你的问题是从一个文件中导出更多值并将它们导入到另一个文件中,答案是不要使用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
export
和 export default
之间的区别在这里得到了很好的描述:Typescript export vs. default export(不管问题是关于 TypeScript)。
【讨论】:
@kazuyahiko 如果答案正确,请考虑将其标记为已接受以上是关于在一个 reducer 文件 REDUX 中导出多个变量的主要内容,如果未能解决你的问题,请参考以下文章
OBIEE 11 - 如何在没有提示的情况下在 excel 中导出多页仪表板