Skip to content

onPress handler on Button component has a return type of void only #4392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mounis-bhat opened this issue Apr 16, 2023 · 2 comments
Closed

Comments

@mounis-bhat
Copy link

📝 Feedback

The onPress handler on Button component has a return type of void however many tasks done using buttons can be asynchronous and the expect the type of promise returning function.

🔦 Context

Faced this issue when calling an API with a function and es-lint was throwing the following error:

Promise-returning function provided to attribute where a void return was expected.eslint@typescript-eslint/no-misused-promises

💻 Code Sample

async function loginWithGoogle() {
    setIsLoading(true)
    try {
      await signIn()
    } catch (e) {
      toast.error("Something went wrong")
    } finally {
      setIsLoading(false)
    }
  }
<Button
  className="mx-auto w-full max-w-sm"
  onPress={loginWithGoogle}
  isDisabled={isLoading}
>
  {isLoading ? (
    <Loader2 className="mr-2 h-5 w-5 animate-spin " />
  ) : (
    <GoogleSvg />
  )}
  <span>Google</span>
</Button>

🌍 Your Environment

| react-aria-components |
| Edge Chromium |
| Windows 11 |

🧢 Your Company/Team

Independent Developer

@LFDanLu
Copy link
Member

LFDanLu commented Apr 17, 2023

I tried broadening the the onPress return type to void | Promise<void> but this seems to have some known issues that would make it a no go it seems. Perhaps changing the return type to unknown?

@snowystinger
Copy link
Member

I don't think we necessarily need to change anything, this is apparent in React as well. typescript-eslint/typescript-eslint#4619 The recommendation is that when using React, make use of the options to turn off certain checks. Specifically checksVoidReturn. Going to close with the official recommendation. We can reopent if that changes down the road.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants