useLink
ts
ts
A low-level hook that lets you create your own accessible Link component.
This is inspired by useLinkProps from React Navigation.
Usage​
tsx
tsx
Arguments​
- href: required The URL to link to. Either a- stringor- Urlobject.
- as: The URL to link to. Either a- stringor- Urlobject.
- shallow:- boolean
See the Next.js Link API for more details about these props.
Returns​
The hook returns the following values:
- href:- string
- onPress:- function
- accessibiltyRole:- string
You should spread these values directly onto your component:
tsxMyLink = ({href ,as , ...props }:Props ) => {constlinkProps =useLink ({href ,as ,})Âreturn <Pressable {...props } {...linkProps } />}
tsxMyLink = ({href ,as , ...props }:Props ) => {constlinkProps =useLink ({href ,as ,})Âreturn <Pressable {...props } {...linkProps } />}
Full Example​
Here is an example of useLink, together with MotiPressable from moti.
tsxMotiPressableProps ,MotiPressable } from 'moti/interactions'import {useLink ,UseLinkProps } from 'solito/link'Âexport typeMotiLinkProps =UseLinkProps &Omit <MotiPressableProps ,// ignore props that will be overridden by useLinkkeyofUseLinkProps | keyofReturnType <typeofuseLink >>Âexport constMotiLink = (props :MotiLinkProps ) => {constlinkProps =useLink (props )Âreturn <MotiPressable {...props } {...linkProps } />}
tsxMotiPressableProps ,MotiPressable } from 'moti/interactions'import {useLink ,UseLinkProps } from 'solito/link'Âexport typeMotiLinkProps =UseLinkProps &Omit <MotiPressableProps ,// ignore props that will be overridden by useLinkkeyofUseLinkProps | keyofReturnType <typeofuseLink >>Âexport constMotiLink = (props :MotiLinkProps ) => {constlinkProps =useLink (props )Âreturn <MotiPressable {...props } {...linkProps } />}
This code snippet is the source code for Solito's Moti integration 🤯
You can now use MotiLink as if it were MotiPressable, along with the props for our link:
tsx
tsx