kazuyahiko
kazuyahiko

Reputation: 343

export multiple variable in one reducer file REDUX

i want to ask how to export multiple const in 1 file reducers in redux

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)

then how to import that reducers in main reducer that combine all the file, what i did right now is to split ever const of my reducers in 1 file, and this is not efficient,

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

Upvotes: 2

Views: 4764

Answers (1)

Marek Dorda
Marek Dorda

Reputation: 1295

I am not sure what you want to achive, but if your problem is to export more values from one file and import them to another file, the answer is not to use export default, but classic exports:

myreducers.js

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

and then import them

mainreducer.js

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

The difference between export and export default is very well described here: Typescript export vs. default export (does not matter the question is about TypeScript).

Upvotes: 6

Related Questions