0

useEffect(login, [])

Where login returns a promise. I don't need the return, I just want it to fire.

Gives the following Typescript errors:

Argument of type '() => Promise<void>' is not assignable to parameter of type 'EffectCallback'.
  Type 'Promise<void>' is not assignable to type 'void | Destructor'.t

Yet, this does not throw errors:

useEffect(() => {
    login()
  }, [])

Why does the first throw TS errors?

2
  • 2
    Because login returns Promise<void>, not void or Destructor Commented May 12, 2022 at 0:06
  • Besides the arrow function, I now also use react-use useAsync just for that... Commented May 12, 2022 at 5:11

1 Answer 1

2

It is technically different returns, while your login returns a promise, useEffect does expect the EffectCallback to be met, which is basically to return a function that is called once the component is being unmounted or void (nothing).

To give a more step-by-step type of answer:

  • useEffect accepts a function that is called EffectCallback in typescript terms.
  • That EffectCallback cannot by anything else than a plain function.
  • It should return void (nothing) or Destructor which is a plain function called once the component is being unmounted.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.