0

Below is a code snippet which I came across in one of the blog for redux.

enter image description here

This snippet explains redux-thunk. But I am trying to make sense out of the weird syntax

return ({dispatch, getState}) => next => action =>{}

I tried a google search with this syntax but did not get much help. I am not understanding how next and action objects are getting their values. Can someone please help me understand what's going on here?

3
  • 2
    stackoverflow.com/questions/32782922/… may be this will help. Commented Feb 17, 2020 at 14:22
  • ... => ... is always the definition of a function which takes some parameter(s) and returns something. This just happens a few times in a row here. You have a function that returns a function that takes two parameters that returns a function that takes one parameter that returns a function that takes one parameter that returns the result of a call to action or perhaps next. Commented Feb 17, 2020 at 14:24
  • 1
    Why not upload images of code on SO when asking a question? Commented Feb 17, 2020 at 14:25

1 Answer 1

3

Chaining of functions in this way is usually to enable a user to bind arguments to a final function in a controlled fashion, rather than having to supply them all in one go.

In effect it is an API design choice for a piece of functionality.

({dispatch, getState}) => next => action => {}

...is equivalent to:

function({dispatch, getState}) { // destructure two properties into arguments
    return function(next) {
        return function(action) {
            // this function has access to `dispatch`, `getState`, `next` & `action`
        }
    }
}

Note that the "binding" of the arguments to the inner functions occurs through a feature of JavaScript called closures. Full explanation of closures in JavaScript here.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.