Redux: Redux compose "TypeError: undefined is not an object (evaluating 'last.apply')"

Created on 13 Oct 2016  路  11Comments  路  Source: reduxjs/redux

Hey guys. I'm just building the dependencies in a project made with react/redux. And when webpack bundle the main file, the error: "TypeError: undefined is not an object (evaluating 'last.apply')" has been shown on console log.

function compose() {
  for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) {
    funcs[_key] = arguments[_key];
  }

  if (funcs.length === 0) {
    return function (arg) {
      return arg;
    };
  }

  if (funcs.length === 1) {
    return funcs[0];
  }

  var last = funcs[funcs.length - 1];
  var rest = funcs.slice(0, -1);
  return function () {
    return rest.reduceRight(function (composed, f) {
      return f(composed);
    }, last.apply(undefined, arguments));
  };
}

Is there any solution for this error?

Most helpful comment

Fix

const middleware = applyMiddleware(
    routerMiddleware(browserHistory),
    thunkMiddleware,
    authStateMiddleware
);

const composeEnhancers =
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const store = createStore(
    rootReducer,
    composeEnhancers(middleware)
);

All 11 comments

That sounds as if you're passing in something invalid when creating your store. Please check your store creation code and verify that the call to compose has valid enhancers as arguments.

@igorrfc were you able to figure out steps to reproduce? Wondering if related to this https://github.com/reactjs/redux/pull/2145

In my case caused because of Redux Dev Tools. By passing undefined to compose it generates this error

createStore(
    combineReducers({ auth, missions, artworks, artists }),
    compose(
        applyMiddleware(promiseMiddleware),
        __REDUX_DEVTOOLS_EXTENSION__ && __REDUX_DEVTOOLS_EXTENSION__()
    ),
);

I got this error from safari and I was able to fix it with this.

// Only chrome can handle the redux dev tool
// redux compose cannot handle a null or undefined middleware
if (window.navigator.userAgent.includes('Chrome')) {
  var store = createStore(
    reducer,
    initialState,
    compose(
      applyMiddleware(
        routerMiddleware(browserHistory)
      ),
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    )
  );
} else {
  var store = createStore(
    reducer,
    initialState,
    compose(
      applyMiddleware(
        routerMiddleware(browserHistory)
      )
    )
  );
}

Fix

const middleware = applyMiddleware(
    routerMiddleware(browserHistory),
    thunkMiddleware,
    authStateMiddleware
);

const composeEnhancers =
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const store = createStore(
    rootReducer,
    composeEnhancers(middleware)
);

The above solution from @ballerabdude worked but the linter complained about store being created twice and a redundant statement. I was able to get this working to remove the linter warnings ...

var getComposeEnhancers = () => {
    if (window.navigator.userAgent.includes('Chrome')) {
      return redux.compose(
        redux.applyMiddleware(thunk)
        ,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
      );
    }
    return redux.compose(redux.applyMiddleware(thunk) );
  };

  var store = redux.createStore(reducers, initialState, getComposeEnhancers() );
  return store;

Might help someone in the future...

const createStoreWithMiddleware = createStore(
  rootReducers,
  compose(
    applyMiddleware(thunk, logger),
    window.navigator.userAgent.includes('Chrome') ?
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() : compose,
  ),
);

@WebbizAdmin what does compose by itself?

@casvil Hey, it allows to apply enhancements to the store, like the devtools above. Nice explenations are Stackoverflow and Redux docs .

I actually changed to this package Redux Dev Tools Extention

import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
....
....
....
const createStoreWithMiddleware = createStore(
  rootReducers,
  composeWithDevTools(applyMiddleware(thunk)),
);

@Barlog-M's solution worked for me. Thank you!

@barlog-m, this workaround is clean and worked perfect for me, thank you very much

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cloudfroster picture cloudfroster  路  3Comments

olalonde picture olalonde  路  3Comments

amorphius picture amorphius  路  3Comments

captbaritone picture captbaritone  路  3Comments

mickeyreiss-visor picture mickeyreiss-visor  路  3Comments