T
T
Toma2019-01-30 13:49:35
React
Toma, 2019-01-30 13:49:35

What is the best way to pass dispatch to Redux (React)?

There is a function, and now a piece of code appears in it, which needs to be moved into a separate function. Well, the trick is that this is the action function. and in that section of code, dispatch is used. What is the best way to pass dispatch?
dispatch(subFunc(data)) or subFunc(dispatch, data);

const mainAction = (data) => {
    return function (dispatch) {
          subFunc(dispatch, data);
          //или
          dispatch(subFunc(data));
    }   
  };

 const subFunc= ((dispatch), data) => {
      //......
     dispatch(closeLoader())
     //......
  };

maybe a 3rd option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Okhotnikov, 2019-01-30
@tomatenshi

const func = data => (
  dispatch => {
   //......
    dispatch(action());
  //......
  }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question