0

Below function is the "dispatch" is a parameter or a something else. It will be helpful if someone can describe the theory behind this. As well as the traditional way of writing below function.

export const incrementCount = () => dispatch => {
    return dispatch({ type: actionTypes.INCREMENT })
};
FarukT
  • 1,560
  • 11
  • 25
  • https://stackoverflow.com/questions/47002774/what-does-dispatch-mean-do-and-why-is-it-used-when-we-have-then-and-cat – Mohamed Elrashid Jan 13 '19 at 13:50
  • I need to know on how "dispatch" word behaves. NOT ON how redux works – thili sadunika Jan 13 '19 at 13:52
  • do you ask what is an arrow function or what does dispatch or what is redux thunk? if it's not the first one, then I suggest you read *any* basic article on how redux works because what dispatch does is actually can be translated to what is the flow of redux. – Stav Alfi Jan 13 '19 at 13:54
  • `incrementCount` is a function which returns a function, this last function takes as input a function called `dispatch` and inside it executes this dispatch function and returns the result of this execution. – quirimmo Jan 13 '19 at 13:56
  • Please add framework tag and then change the title to a relevant question like "How does dispatch work in this code" – mplungjan Jan 13 '19 at 13:57

1 Answers1

0

In the code above incrementCount is a function returning a function that accepts dispatch as a parameter and that returned function is returning a dispatch method

export const incrementCount () {
  return function(dispatch) {
      return dispatch({ type: actionTypes.INCREMENT })
   }
}
Sahil Raj Thapa
  • 2,353
  • 3
  • 12
  • 23
  • Thanks. So this has been called by dispatch(incrementCount()). So can you explain how this works and how dispatch parameter is passed to the function that you have mentioned. – thili sadunika Jan 13 '19 at 14:03
  • you are using`redux-thunk` as middleware. when `dispatch(incrementCount()` is called it is caught by `redux-thunk` instead of `redux` and the returned function taking dispatch as argument is also handled by `redux-thunk`. – Sahil Raj Thapa Jan 13 '19 at 14:15