GN.
GN.

Reputation: 9839

useEffect promise throwing Typescript errors

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?

Upvotes: 0

Views: 725

Answers (1)

Marlom
Marlom

Reputation: 688

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.

Upvotes: 2

Related Questions