Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions src/app/navigation/use-link.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback } from 'react'
import { GestureResponderEvent, Platform } from 'react-native'

import { useRouter } from './use-router'
Expand All @@ -13,39 +14,40 @@ export function useLink({
const router = useRouter()

// https://github.com/react-navigation/react-navigation/blob/main/packages/native/src/useLinkProps.tsx#L64
const onPress = (
e?: React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent
) => {
let shouldHandle = false
const onPress = useCallback(
(e?: React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent) => {
let shouldHandle = false

if (Platform.OS !== 'web' || !e) {
shouldHandle = e ? !e.defaultPrevented : true
} else if (
!e.defaultPrevented && // onPress prevented default
// @ts-expect-error: these properties exist on web, but not in React Native
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys
// @ts-expect-error: these properties exist on web, but not in React Native
(e.button == null || e.button === 0) && // ignore everything but left clicks
// @ts-expect-error: these properties exist on web, but not in React Native
[undefined, null, '', 'self'].includes(e.currentTarget?.target) // let browser handle "target=_blank" etc.
) {
e.preventDefault()
shouldHandle = true
}

if (shouldHandle) {
if (href === '#') {
// this is a way on web to stay on the same page
// useful for conditional hrefs
return
if (Platform.OS !== 'web' || !e) {
shouldHandle = e ? !e.defaultPrevented : true
} else if (
!e.defaultPrevented && // onPress prevented default
// @ts-expect-error: these properties exist on web, but not in React Native
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys
// @ts-expect-error: these properties exist on web, but not in React Native
(e.button == null || e.button === 0) && // ignore everything but left clicks
// @ts-expect-error: these properties exist on web, but not in React Native
[undefined, null, '', 'self'].includes(e.currentTarget?.target) // let browser handle "target=_blank" etc.
) {
e.preventDefault()
shouldHandle = true
}
if (replace) {
router.replace(href, { experimental })
} else {
router.push(href)

if (shouldHandle) {
if (href === '#') {
// this is a way on web to stay on the same page
// useful for conditional hrefs
return
}
if (replace) {
router.replace(href, { experimental })
} else {
router.push(href)
}
}
}
}
},
[router, href, replace, experimental],
)

return {
Copy link
Author

@pierpo pierpo Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also add a useMemo here, it might be useful because I have to add the following piece of code in my app haha

  const stableLinkProps = useMemo(
    () => ({
      onPress: linkProps.onPress,
      accessibilityRole: linkProps.accessibilityRole,
      href: linkProps.href,
    }),
    [linkProps.onPress, linkProps.accessibilityRole, linkProps.href],
  )

accessibilityRole: 'link' as const,
Expand Down