Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: middleware is not a function

I'm trying to apply redux in my reactjs app. I can't proceed because of these errors:

enter image description here

enter image description here

I'm sure that I already installed all the dependencies that I need. Here is a relevant part of my package.json

"dependencies": {    "react-redux": "^5.0.6",    "redux": "^3.7.2",    "redux-logger": "^3.0.6",    "redux-promise": "^0.5.3",    "redux-thunk": "^2.2.0", } 

Here is a part of my index.js that implements redux

import { createStore, applyMiddleware } from 'redux'; import { Provider } from 'react-redux'; import thunkMiddleware from 'redux-thunk'; import createLogger from 'redux-logger'; import reducers from './reducers';  const logger = createLogger(); const store = createStore(reducers,     applyMiddleware(         thunkMiddleware, logger     ) )  ReactDOM.render(     <Provider store={store}>         <App />     </Provider>,     document.getElementById('root') ); 
like image 733
James Solomon Belda Avatar asked Oct 22 '17 01:10

James Solomon Belda


People also ask

What is Applymiddleware?

middleware) Middleware is the suggested way to extend Redux with custom functionality. Middleware lets you wrap the store's dispatch method for fun and profit. The key feature of middleware is that it is composable.

What is thunk in react JS?

The word "thunk" is a programming term that means "a piece of code that does some delayed work". Rather than execute some logic now, we can write a function body or code that can be used to perform the work later.

What is redux promise?

redux-promise "teaches" dispatch how to accept promises, by intercepting the promise and dispatching actions when the promise resolves or rejects. Normally, dispatch returns whatever action object was passed in. Because middleware wrap around dispatch , they can also change what value is being returned.


2 Answers

According to the docs you are mixing up the usage of redux-logger

You either need to import the specific createLogger function

import { createLogger } from 'redux-logger'  const logger = createLogger({   // ...options }); 

Or use the default import

import logger from 'redux-logger' 

And then your code should be fine

const store = createStore(   reducers,   applyMiddleware(thunkMiddleware, logger) ) 
like image 163
Balázs Édes Avatar answered Oct 02 '22 12:10

Balázs Édes


import logger from 'redux-logger'; import thunk from 'redux-thunk'; import reduxPromiseMiddleware from 'redux-promise-middleware'; import { applyMiddleware, createStore } from 'redux';  const middleware = applyMiddleware(reduxPromiseMiddleware, thunk, logger); const store = createStore(reducer, middleware); 
like image 40
harun ugur Avatar answered Oct 02 '22 14:10

harun ugur