I am trying to learn a react-redux. And in order to call the external rest API, I am using thunk middleware.
However the syntax to so confuses me a lot, I would appreciate if someone can explain me.
export function listTodos() {
return (dispatch) => axios.get("http://localhost:5001/todos")
.then(response => {
console.log(response.data);
dispatch(getTodos(response.data));
})
}
export function _listTodos(todos) {
return {
type: 'LIST_TODOS',
todos: todos
}
}
so in order to get the response from the external API, I am calling listTodos function, which dispatches an action returned by listTodos function.
The part confuses me is Syntax (maybe because it's ES6 an I am not very familiar with that).
how come return is taking a parameter, dispatch?
from where this dispatch is coming, I am not passing anything when I call
listTodoswhy do I even need a return, shouldn't dispatching an action enough from within
then
dispatchis passed in by the middleware. No, it is not enough because you need to have thedispatchargument in scope which is only passable via a wrapped arrow function (which is callable by the middlware) - Andrew Lireturn (dispatch) =>is actuallyreturn function (dispatch) {correct? - Gaurang Shah