A
A
Andrey Okhotnikov2019-06-11 20:32:21
React
Andrey Okhotnikov, 2019-06-11 20:32:21

How to use useDispatch with redux-thunk?

I use new hooks from react-redux - useDispatch .
How can I call my asynchronous function through this hook in which dispatch will be available, like that

const func = (data) => async (dispatch) => {
//code...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-11
@tsepen

dispatch your function is derived from redux-thunk. Here .
UseDispatch example:

const Foo = ({ data }) => {
  const dispatch = useDispatch();

  useEffect(() => {
    dispatch(func(data));
  }, [data]);
  
  return (/* ... */);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question