L
L
lexstile2021-08-11 12:47:07
redux
lexstile, 2021-08-11 12:47:07

Difference in approaches - store.dispatch vs middleware?

Do I understand correctly?
We have an asynchronous action in a separate file. Can we import store and use store.dispatch ?
And we can use middleware - the same redux thunk and accept the same dispatch in the arguments:

function loadSomeData(userId) {
  return dispatch => fetch(`http://data.com/${userId}`)
    .then(res => res.json())
    .then(
      data => dispatch({ type: 'LOAD_SOME_DATA_SUCCESS', data }),
      err => dispatch({ type: 'LOAD_SOME_DATA_FAILURE', err })
    );
}


Which approach is better and why? (if I understood everything correctly)
We can also pass dispatch as an argument (but not about that now):
function loadSomeData(dispatch, userId) {
  return fetch(`http://data.com/${userId}`)
    .then(res => res.json())
    .then(
      data => dispatch({ type: 'LOAD_SOME_DATA_SUCCESS', data }),
      err => dispatch({ type: 'LOAD_SOME_DATA_FAILURE', err })
    );
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question