3

I am setting up a simple React/Redux app. Here is my setup:

var {createStore, applyMiddleware } = require('redux')
var thunkMiddleware = require('redux-thunk')
var store = createStore(
  reducers,
  applyMiddleware(thunkMiddleware)
)

class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <HashRouter>
          <div className="app">
            <Route exact path='/' component={Home}/>
            <Route path='/Test' component={Test}/>
          </div>
        </HashRouter>
      </Provider>
    );
  }
}

Also, here are my reducers:

var {combineReducers} = require('redux')

const carlist = (state=[], action) => {
  switch (action.type){
    case 'LOAD':
      return action.test
    default:
      return state
  }
}
module.exports = combineReducers({carlist})

When I run my app, I get this error:

applyMiddleware.js:39 Uncaught TypeError: middleware is not a function

What am I missing?

2
  • One possibility is that you forgot npm install --save redux-thunk ? Commented May 20, 2017 at 2:07
  • No, I thought about that and I checked. I even added a console.log(thunkMiddleware) for a sanity check and it logged out Commented May 20, 2017 at 2:09

3 Answers 3

5

the problem looks like it might be here:

var thunkMiddleware = require('redux-thunk')

Your middleware -- here thunkMiddleware -- is not a function, suggesting that it is not being pulled from the package. You should try:

var thunkMiddleware = require('redux-thunk').default

Or, try using ES6 module syntax (since you are already using ES6 features):

import thunkMiddleware from 'redux-thunk'

Sign up to request clarification or add additional context in comments.

Comments

2

I had this error when using redux-logger

I had just upgraded my packages and change

import createLogger from 'redux-logger'

to

import { createLogger } from 'redux-logger'

Comments

0

Here is what worked for me - First reference the redux-thunk

const thunkMiddleware = require('redux-thunk')

Secondly, when adding middleware use following syntax.

const store = createStore(reducer, applyMiddleware(thunkMiddleware.thunk))

Here are version details i'm working on -

"redux-thunk": "^3.1.0"

"redux": "^5.0.1",

Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.