Skip to content
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

Got "onClick is deprecated, please use onPress" while no onClick is used #3196

Closed
wingkwong opened this issue Jun 6, 2024 Discussed in #3193 · 12 comments · Fixed by #3569
Closed

Got "onClick is deprecated, please use onPress" while no onClick is used #3196

wingkwong opened this issue Jun 6, 2024 Discussed in #3193 · 12 comments · Fixed by #3569
Assignees

Comments

@wingkwong
Copy link
Member

Discussed in #3193

Originally posted by bllakcn June 6, 2024
My first time using the as prop to render a Link component as a Button.
I see this in my console
onClick is deprecated, please use onPress from useLink.

Is there anything I did wrong on my side?

<Button
  as={Link}
  color='primary'
  onPress={doSomething}
  href={/}
  >
  Go somewhere
</Button>
Copy link

linear bot commented Jun 6, 2024

@wingkwong
Copy link
Member Author

The error was thrown from useLink.ts from react-specturm.

onClick: (e: React.MouseEvent<HTMLAnchorElement>) => {
  pressProps.onClick?.(e);
  if (deprecatedOnClick) {
    deprecatedOnClick(e);
    console.warn('onClick is deprecated, please use onPress');
  }

@chirokas
Copy link
Contributor

chirokas commented Jun 6, 2024

@wingkwong, I agree.

We have no warning here.

onClick: (e: React.MouseEvent<HTMLButtonElement>) => {
deprecatedOnClick?.(e);
},

@wingkwong
Copy link
Member Author

@chirokas the issue here is that onPress is already used, but the warning is still asking you to use onPress.

@chirokas
Copy link
Contributor

chirokas commented Jun 6, 2024

@wingkwong, onClick is passed to Link in getButtonProps.

@wingkwong
Copy link
Member Author

We still allow users to pass onClick. However, if you look at the code, there is no onClick function passing to Button. Do you wanna investigate this issue? I could assign you if you wish.

@chirokas
Copy link
Contributor

chirokas commented Jun 6, 2024

Well, I'm willing to try.

@wingkwong
Copy link
Member Author

feel free to ping me in discord if you need help.

@chirokas
Copy link
Contributor

chirokas commented Jun 6, 2024

@wingkwong :

The pressProps returned by the usePress Hook (in the useAriaButton Hook) always have an onClick prop. This will be passed to the Link component.

https://github.com/adobe/react-spectrum/blob/5548b8925798970db9ffeb1a84049b3afc6bc040/packages/%40react-aria/interactions/src/usePress.ts#L303

We need a private prop to represent the author's (non-user) onClick function!?

@wingkwong
Copy link
Member Author

Good finding. Apart from pressProps.onClick from usePress, there is one more in buttonProps.

onClick: (e: React.MouseEvent<HTMLButtonElement>) => {
  deprecatedOnClick?.(e);
},

We need a private prop to represent the author's (non-user) onClick function!?

I'm not sure what you mean. Mind to elaborate more?

@wingkwong
Copy link
Member Author

interestingly I didn't see that warning on my another branch. The difference is below. Maybe another direction to explore this issue.

devDependencies:
- @react-types/shared 3.23.1
+ @react-types/shared 3.22.1

@awesome-pro
Copy link
Contributor

@chirokas You have not solved this issue for a long time, so I solved it.
I hope you don't have any bad about it :)

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

Successfully merging a pull request may close this issue.

3 participants