npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-use-breadcrumbs

v1.1.1

Published

Automatic breadcrumbs for react-router

Downloads

115

Readme

react-router-use-breadcrumbs

React hooks that generate a breadcrumb trail, for use with React Router.

Version badge Minified badge Minzipped badge Dependencies badge Types badge NodeJS CI

It is simple, flexible, and convenient.

Installation

npm install react-router-use-breadcrumbs

This package has peer dependencies of react-router-dom v6 and react v16.8+. Its only direct dependency is nanoid.

It is not compatible with react-router v5, or older versions.

Usage

This package's <BreadcrumbProvider> component should be placed at the root of your application.

Any component that you wish to render a breadcrumb for should call the useBreadcrumb hook, passing in text or a component to display. The breadcrumb's url will be calculated automatically.

To access a breadcrumb trail, call the useBreadcrumbs hook, which returns an array of breadcrumb objects, with to and title fields. Typical usage is to render a <NavLink> component for each of these.

Note that in the case of index <Route /> components, useBreadcrumb should be called by the parent component (i.e. the one containing the <Routes>), rather than the component rendered by the index route. (Otherwise, its breadcrumb will only be active when you browse to the index route.)

Example: simple crumbs

The simplest scenario is to render a <NavLink> for breadcrumb element returned by the useBreadcrumbs hook. You should include the end prop on these, so that they all aren't styled as "active" links.

const breadcrumbs = useBreadcrumbs();

return (
    <ul>
        {breadcrumbs.map((crumb) => (
            <li key={crumb.to.pathname}>
                <NavLink to={crumb.to} end>{crumb.title}</NavLink>
            </li>
        ))}
    </ul>
);

Example: separate title

The "last" crumb's title can be thought of as the title of the current page. You might wish to render it separately from the "main" breadcrumb trail.

This can be done using a single call to the useBreadcrumbs hook, or by multiple calls from separate components.

const breadcrumbs = useBreadcrumbs();

const [currentCrumb] = breadcrumbs.splice(-1, 1);

return (
    <>
        <ul>
            {breadcrumbs.map((crumb) => (
                <li key={crumb.to.pathname}>
                    <NavLink to={crumb.to} end>{crumb.title}</NavLink>
                </li>
            ))}
        </ul>

        <h1>{currentCrumb?.title}</h1>
    </>
);

To take this a step further, you might wish to display different text when rendering a crumb as the page title, compared to when rendering it in the breadcrumb trail. To support this, you could pass a "page title" to useBreadcrumb as the second data parameter, then use this when rendering:

useBreadcrumb('Crumb Title', 'Page Title');

// ...

<h1>{currentCrumb?.data ?? currentCrumb?.title}</h1>

Example: go up one level

If the "last" crumb refers to the current page, then the "second last" crumb refers to its parent. Rendering just that breadcrumb will provide a link to "go up one level."

const breadcrumbs = useBreadcrumbs();

return breadcrumbs.length > 1
    ? <NavLink to={breadcrumbs[breadcrumbs.length - 2].to} end>Go Up</NavLink>
    : <span>You are at the top level</span>

API

BreadcrumbProvider

This component provides a context that the breadcrumb hooks need in order to function. All calls to useBreadcrumb and useBreadcrumbs should be made from descendants of this provider.

useBreadcrumb

Any component that should render a breadcrumb should call this hook.

Doing so adds a crumb into the breadcrumb trail, which will be removed again if the component calling this hook stops rendering.

  • title (string|component): Text or component to display for this breadcrumb.
  • data (any): Optional extra data that you wish to associate with this breadcrumb.

Simple scenarios would pass a string as a breadcrumb's title, but the following are all valid:

useBreadcrumb('Home');
useBreadcrumb(<span title="Hovered!">Home</span>);
useBreadcrumb(<CustomComponent title="Home" icon="house" />);

The data field is intended to support additional logic or considerations when breadcrumbs are being rendered. It won't be used in most scenarios.

useBreadcrumbs

This hook returns an array of the currently active breadcrumbs. The array's elements have 3 fields:

  • to (object): A history path object, with pathname and optional search and hash string fields.
  • title (string|component): Text or component to display for this breadcrumb.
  • data (any): Optional extra data that you wish to associate with this breadcrumb.

The title and data values are what was passed into the breadcrumb's corresponding useBreadcrumb call. The to value is calculated automatically, and can be passed directly to the to prop of a <NavLink> component.