|Kit

useLinkComponent

Resolves your router's Link from LinkComponentProvider, so a component of your own renders client-side links the same way NavLink and Spotlight do.

No component in the kit imports a router. Instead, you wrap the app once in LinkComponentProvider with your router's Link (TanStack Router's, Next.js's, React Router's), and every component that renders a link resolves it from there. NavLink and Spotlight do. useLinkComponent is how a component of your own does the same.

import { useLinkComponent } from '@42-central/ui-react';

function CrumbLink({ linkComponent, ...props }) {
  const Link = useLinkComponent(linkComponent) ?? 'a';
  return <Link {...props} />;
}

Usage

The provider goes at the app root, next to your router:

import { LinkComponentProvider } from '@42-central/ui-react';
import { Link } from '@tanstack/react-router';

<LinkComponentProvider component={Link}>
  <App />
</LinkComponentProvider>

In this demo, RouterLink stands in for a router's Link: it records where it would navigate instead of loading a page. Turn the provider off and the same CrumbLink falls back to a plain <a>.

Links render through RouterLink. Click one.

Code

function CrumbLink(props) {
  const Link = useLinkComponent() ?? 'a';
  return <Link {...props} />;
}

<LinkComponentProvider component={RouterLink}>
  <CrumbLink href="#settings">Settings</CrumbLink>
  <CrumbLink href="#profile">Profile</CrumbLink>
</LinkComponentProvider>

API

Prop

Type

Returns the override, else the nearest provider's component, else undefined. Fall back to your own default tag (?? 'a') when it's undefined. The precedence is the same as useLocale's: prop, then provider, then default.

LinkComponentProvider

Prop

Type

On this page