KDC Menu

Saturday, April 28, 2018

TypeError: Undefined: Redux 4

Redux 4 > createStore > compose

The Error

            TypeError: Cannot read property 'apply' of undefined
            [node_modules/redux/es/redux.js:523]
        

The Problem and Solution

If you've gone through a tutorial in which they suggest adding the Redux Dev Tools in your store.js like this:

            window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
        
But they're using it inside of a compose(), you could receive a Type Error for an undefined when the extension is not enabled (or present). The correct way to implement the Redux Dev Tools within a compose is as follows:

            window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
        
I learned this the hard way, and was eventually brought rescue from GitHub user [zalmoxisus].
zalmoxisus also references the docs for this: [1.2 Advanced store setup (middleware and enhancers)]

Code Extract from [store.js]

      compose(
        applyMiddleware(...middleware),
        window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
        window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
      )
    

A Little Error Verbosity

No comments:

Post a Comment

All comments are typically approved within an hour or two of posting.