-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
usePress
/ useLink
is not compatible with NextJS's next/link
#2525
Comments
Hey! thanks for opening this, it can be a little bit of work to get some other libraries to work with react spectrum, but you should be able to address this by changing your Button implementation to
It should be safe to pass a dummy preventDefault because we already call it in usePress in most cases. |
Hey Robert, sorry for getting back to this so late, but your suggestion does work! I think it's a pretty nice "workaround" to fake the event for consumers that need it, so I'll close this. Thanks so much! |
@snowystinger |
If you're using a button, then how is a full reload happening? Buttons don't have a default behavior to navigate. If you used an anchor element, I could see that happening. |
@snowystinger |
Bringing it up with the team, I'll reopen for now for more visibility. |
I've read this in many many places by now, but it's really not clear when I'm trying to understand I wonder if it should be:
cc: @devongovett |
@snowystinger is there any news for this issue? I really like react-aria lib but to be able to use it in our projects we would need a support for nextjs link to work without a full reload. Thank you! |
Sorry, I lost this thread, thanks for the reminder and patience. I'll remember to bring it up this week. So unfortunately it won't quite be as simple as adding that OR statement. Doing that causes checkboxes, switches, and radios to fail.
|
Hi. Is there any chance there's an update on this? 😄 |
There is a solution if you're using react-aria, you can preventDefault yourself. Here's a codesandbox based on the one in the description |
I'm facing the same issue with |
Buttons that navigate to a new page should not be buttons, they should be links. That way they are announced by assistive technology as links. They can be styled to look like buttons if you want, but they should use the In your example, you have a I would suggest moving the Here's a forked version of your sandbox with the changes applied: https://codesandbox.io/s/react-aria-issue-forked-u7llrm?file=/src/Components/Button/index.js |
Thanks @devongovett . I discovered the same as well in #963. I've restructured my navigation in the following sandbox demo to avoid buttons in anchor links. https://codesandbox.io/s/react-aria-issue-forked-1nqk27?file=/src/Pages/Layout.js. |
Good OP, thanks for the detailed description of what's going on. I would be less apologetic, however, in calling out this use-case. Seems evident from the discussion here that implementing a bespoke Could we do something like extend |
🐛 Bug Report
Because of NextJS's
next/link
component's special behaviour, custom components using theusePress
(oruseLink
) do not work the way I'd expect🤔 Expected Behavior
When I use a component built with react-aria hooks as a child of
next/link
, I expect successful client-side navigation to the destination😯 Current Behavior
Depending on the usage, either I see an error being thrown, or I see successful server-side navigation (a full page reload)
This is the error I see (you'll be able to see it too in the CodeSandbox example I've linked)
💁 Possible Solution
React-aria's
PressEvent
could expose properties available on the nativeMouseEvent
. Howver, I'm not sure about the feasibility / consequences of this🔦 Context
We use React-aria in our design system for building a
Button
component. In our app (which uses NextJS), some buttons only take you to a different page on the app, so I treat them as links (that just happen to look like buttons)In Next, the recommended way to use links is via their
next/link
component. That component injects anonClick
prop to its first child, and thisonClick
callsevent.preventDefault
to be able to do client-side navigation (AKA navigation without a full page refresh)I expect to be able to use my
Button
this way:However, this throws an error (you should be able to see this in the CodeSandbox I've linked below).
I understand this is quite a niche use-case, and I'm not even sure if react-aria can / should handle this. I can't imagine where else this problem would pop up, so as far as I know, this issue is limited to
next/link
.I have investigated the issue and do have ways to get around this but the solution defeats the purpose of using react-aria in the first place, so it is not ideal. I'm happy to explain in detail exactly what's going on if you'd like (in Next and react-aria), but I'm not sure if that's relevant / required right now.
For some more context, here is Next's
next/link
component: https://github.com/vercel/next.js/blob/5e185fc5da227801d3f12724be3577f4a719aa69/packages/next/client/link.tsx💻 Code Sample
Here is a minimal repro:
https://codesandbox.io/s/next-link-react-aria-incompatibility-l0k0j?file=/pages/index.jsx
The idea is that navigating between the
/
&/about
pages should not reload the page.You'll see that I have two components on each page. The
Button
uses theusePress
hook, and theLink
uses theuseLink
hook.The
Button
crashes, while theLink
navigates correctly, but the entire page is reloaded.🌍 Your Environment
🧢 Your Company/Team
🕷 Tracking Issue (optional)
The text was updated successfully, but these errors were encountered: