V
V
vs17102018-08-15 15:37:08
JavaScript
vs1710, 2018-08-15 15:37:08

How does applyMiddleware work?

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';

import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import promiseMiddleware from 'redux-promise'

import Routes from './routes';


const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore);
console.log(createStoreWithMiddleware);

ReactDOM.render(
    <Provider store={createStoreWithMiddleware}>
        <BrowserRouter>
            <Routes />
        </BrowserRouter>
    </Provider>
    , document.getElementById('root'));

Please explain how this line of code works:
const createStoreWithMiddleware = applyMiddleware(promiseMiddleware)(createStore);

Individually, everything is clear, but I can not put it together.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-08-15
@maxfarseer

What exactly is not clear how ()() works?
Is this notation understandable?

function sum(x) {
   return function(y) {
    return x+y
  }
}

sum(1)(2) // 3
sum(1) // Function

In the documentation, look at:
- Arguments
- Returns
promiseMiddleware takes both dispatch and getState as arguments (although it's not written explicitly, like promiseMiddleware(dispatch, getState)).
applyMiddleware(promiseMiddleware) - returns a function, so you can call (parentheses) it, where the createStore argument is passed, which is imported above from the redux library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question