react-spectrum: `usePress` / `useLink` is not compatible with NextJS's `next/link`
š Bug Report
Because of NextJSās next/link componentās special behaviour, custom components using the usePress (or useLink) 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)
Unhandled Runtime Error
TypeError: can't access property "nodeName", e.currentTarget is undefined
linkClicked
node_modules/next/dist/client/link.js (39:27)
onClick
node_modules/next/dist/client/link.js (201:28)
triggerPressEnd
node_modules/@react-aria/interactions/dist/module.js (213:0)
onPointerUp
node_modules/@react-aria/interactions/dist/module.js (427:0)
š Possible Solution
React-ariaās PressEvent could expose properties available on the native MouseEvent. 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 an onClick prop to its first child, and this onClick calls event.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:
import Link from 'next/link'
function TakeMeSomewhereNice() {
return (
<Link href="/somewhere" passHref>
<Button />
</Link>
)
}
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 the usePress hook, and the Link uses the useLink hook.
The Button crashes, while the Link navigates correctly, but the entire page is reloaded.
š Your Environment
| Software | Version(s) |
|---|---|
| @react-aria/interactions | 3.6.0 |
| @react-aria/link | 3.1.4 |
| Browser | Irrelevant |
| Operating System | Irrelevant |
š§¢ Your Company/Team
š· Tracking Issue (optional)
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 6
- Comments: 16 (8 by maintainers)
Iāve read this in many many places by now, but itās really not clear when
usePresspreventDefault the interaction.Iām trying to understand
usePress.tscode and it seems all events doshouldPreventDefaultcheck before callingpreventDefault()but the only one that does not do it is the actualonClick: https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/interactions/src/usePress.ts#L263-L265I wonder if it should be:
cc: @devongovett
@snowystinger Iām facing the same issue trying to use react-routerās useLinkClickHandler for a Button implemented with
useButton(very similar to React Spectrum Button). Your suggestion works to call the click handler in onPress, but the event is not defaultPrevented and therefore a full reload happens. Are you sure preventDefault is already called in most cases? From what I see, preventDefault for click events is called only when the element is disabledGood 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
Linkcomponent (which augments a base HTML anchor element) is essential for any modern-day JS framework. Itās the only way to integrate client-side navigation with core platform attributes (e.g. accessibility, prefetching). And the only way to enable client-side navigation using an<a>tag is to directly call the eventāspreventDefaultmethod.Could we do something like extend
usePresswith a prop likepreventHrefDefault? In this way, it should be possible for a user to directly control href behaviour by setting the flag. And it shouldnāt break other react-aria components.@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!
@snowystinger I should have been more clear. I meant button appearance but as an anchor element. Like how you can render a React Spectrum button as an anchor element via
elementType. Here is a codesandbox trying to create aLinkButtoncomponent inspired by the suggestion from react router docs.preventDefaultbeing a noop function, just prevents the runtime error, but doesnāt prevent the default reload behavior of course. We could even create auseLinkPressHandlerhook, similar touseLinkClickHandler, but that would still require ability to call preventDefault on the original event.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 https://codesandbox.io/s/next-link-react-aria-incompatibility-forked-seom13?file=/pages/about.jsx
Bringing it up with the team, Iāll reopen for now for more visibility.
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. You can find more info on it here https://github.com/adobe/react-spectrum/issues/963