Chained Arrow function syntax

后端 未结 4 1286
野的像风
野的像风 2021-02-19 20:08
const fetch = url => dispatch => {
  // ...
}

export const fetchQuestions = tag => (dispatch) => {
  return dispatch(fetch(tag));
};

What

相关标签:
4条回答
  • 2021-02-19 20:45

    dispatch is the first and single parameter of the function returned by the url => ... function. With normal function syntax, it would be

    const fetch = function(url) {
        return function(dispatch) {...}
    }
    
    0 讨论(0)
  • 2021-02-19 20:49

    Its a shorter way of writing a function that returns another function. The arguments url and dispatch are arguments to the curryed function The ES5 equivalent of arrow function syntax would be

    function fetch(url) {
        return function(dispatch) {
             ....
        }
    }
    

    or with Arrow syntax as

    const fetch = (url) => {
        return (dispatch) => {
            // ... 
        }
    }
    

    Similarly you would have fetchQuestion written as

    export function fetchQuestions(tag) {
         return function(dispatch){
                return dispatch(fetch(tag));
         }
    }
    

    or with Arrow syntax as

    export const fetchQuestions = (tag) => {
        return (dispatch) =>  {
            return dispatch(fetch(tag));
        }
    };
    
    0 讨论(0)
  • 2021-02-19 20:53

    fetch is a named function expression that takes a url parameter and returns a new function that takes a dispatch parameter.

    You could rewrite using traditional function syntax:

    const fetch = function (url) {
      return function(dispatch) {
        // ...
      }
    }
    
    0 讨论(0)
  • 2021-02-19 20:58

    This is equivalent to one function returning another. I.e. this

    const fetch = url => dispatch => {
        // ...
    }
    

    is equivalent to

    const fetch = function(url) {
        return function(dispatch) {
            // ... 
        }
    }
    

    Similarly this

    export const fetchQuestions = tag => (dispatch) => {
      return dispatch(fetch(tag));
    };
    

    is equivalent to

    export const fetchQuestions = function(tag) {
        return function(dispatch) {
            return dispatch(fetch(tag));
        }
    };
    
    0 讨论(0)
提交回复
热议问题