Linked Questions

Popular Questions

react redux accessing external api through thunk middleware

Asked by At

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 listTodos

why do I even need a return, shouldn't dispatching an action enough from within then

Related Questions