What is the type of dispatch clear action?

0

Issue

Here is the current code:

  useEffect(() => {
    dispatch(fetchMovieDetails(movieId!));
    return () => dispatch(fetchMovieDetails(movieId!, "clear"));
  }, [dispatch, movieId]);

Problem is that this code is giving me a very long error:

  • Argument of type ‘() => () => (dispatch: Dispatch) => Promise<null | undefined>’ is not assignable to parameter of type ‘EffectCallback’.
  • Type ‘() => (dispatch: Dispatch) => Promise<null | undefined>’ is not assignable to type ‘void | Destructor’.
  • Type ‘() => (dispatch: Dispatch) => Promise<null | undefined>’ is not assignable to type ‘Destructor’.
  • Type ‘(dispatch: Dispatch) => Promise<null | undefined>’ is not assignable to type ‘void | { [UNDEFINED_VOID_ONLY]: never; }’.
  • Property ‘[UNDEFINED_VOID_ONLY]’ is missing in type ‘(dispatch: Dispatch) => Promise<null | undefined>’ but required in type ‘{ [UNDEFINED_VOID_ONLY]: never; }’.ts(2345)

I’m assuming TypeScript is asking for me to define a return type. So what should I do?

Solution

return () => dispatch(fetchMovieDetails(movieId!, "clear"));

Typescript is pointing out that the cleanup function isn’t supposed to return anything, but with the arrow function shorthand, you’re returning whatever dispatch is returning.

You can fix this by adding in curly brackets to your function, so that there’s no longer an implicit return:

return () => { dispatch(fetchMovieDetails(movieId!, "clear")); };

Answered By – Nicholas Tower

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More