Reputation: 41
I have a root reducer which imports action type constants from the another file. When the reducer is called for the first time because of the createstore, the values of these constants are undefined.
I tried converting those constants into functions and it gave me error- "Object can not be function". When I printed its type, it initially gave me undefined but later calls prints the type- function.
My directory structure is such
helpers/
-index.js
-store.js
-constants.js
reducers/
-index.js
-rootReducer.js
Every index.js file is of type-
export * from './rootReducer'
This is my store file (store.js)-
import { createStore, applyMiddleware, compose } from 'redux'
import { rootReducer } from '../reducers'
import ReduxThunk from 'redux-thunk'
const initialState = {}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
export const store = createStore(
rootReducer,
initialState,
composeEnhancers(applyMiddleware(ReduxThunk))
)
This is my constants file (constants.js)-
export const constants = {
BENCHMARK_DATA_RECEIVED: 'BENCHMARK_DATA_RECEIVED',
BENCHMARK_DATA_FAILED: 'BENCHMARK_DATA_FAILED',
}
This is my root reducer (rootReducer.js)-
import { constants } from '../helpers'
export const rootReducer = (state = [], action) => {
console.log(constants)
// Initially print undefined
// After that prints correct value
switch (action.type) {
case 'BENCHMARK_DATA_RECEIVED':
return { ...state, benchmark_data: action.payload }
default:
return state
}
}
I am no sure about what is causing this problem but it only happens when the reducer is used first time ( most probably while creating store ). I have googled a lot but has not come across any such questions. Maybe there is some problem with my setup. Also printing those constants anywhere (like in action creators) works fine.
Upvotes: 1
Views: 291
Reputation: 10873
To sum up the discussion in the comments, you can import directly from constants.js
in your reducer and meanwhile investigate the file structure you have.
Upvotes: 1