Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
igorrfc opened this issue Oct 13, 2016 · 11 comments
Closed

Comments

@igorrfc
Copy link

igorrfc commented Oct 13, 2016

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?

@markerikson
Copy link
Contributor

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.

@timdorr timdorr closed this as completed Oct 22, 2016
@JoeCortopassi
Copy link
Contributor

@igorrfc were you able to figure out steps to reproduce? Wondering if related to this #2145

@iddan
Copy link

iddan commented Feb 8, 2017

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__()
	),
);

@ballerabdude
Copy link

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)
      )
    )
  );
}

@barlog-m
Copy link

barlog-m commented Mar 16, 2017

Fix

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

const composeEnhancers =
	window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

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

@notgoodwithpowertools
Copy link

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;

@WebbizAdmin
Copy link

WebbizAdmin commented Jan 10, 2018

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,
  ),
);

@casvil
Copy link

casvil commented Jun 9, 2018

@WebbizAdmin what does compose by itself?

@WebbizAdmin
Copy link

@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)),
);

@iMerica

This comment has been minimized.

@alonsoJS

This comment has been minimized.

@reduxjs reduxjs locked as resolved and limited conversation to collaborators May 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests