React Router Breadcrumbs HOC
A tiny (~2kb minified), flexible, higher order component for rendering breadcrumbs with react-router 4.x
site.com/user/id → Home / User / John Doe
Description
Deconstruct a route and return matching breadcrumb components you can render however you like. Render a simple string, a component that fetches a model in order to display the desired content, or just render something totally unrelated to the route.
Install
yarn add react-router-breadcrumbs-hoc
or
npm install react-router-breadcrumbs-hoc --save
Usage
withBreadcrumbs()(MyComponent);
Example
import React from 'react';
import { NavLink } from 'react-router-dom';
import withBreadcrumbs from 'react-router-breadcrumbs-hoc';
const UserBreadcrumb = ({ match }) =>
<span>{match.params.userId}</span>;
const routes = [
{ path: '/users/:userId', breadcrumb: UserBreadcrumb },
{ path: '/example', breadcrumb: 'Custom Example' },
];
const Breadcrumbs = ({ breadcrumbs }) => (
<div>
{breadcrumbs.map((breadcrumb, index) => (
<span key={breadcrumb.key}>
<NavLink to={breadcrumb.props.match.url}>
{breadcrumb}
</NavLink>
{(index < breadcrumbs.length - 1) && <i> / </i>}
</span>
))}
</div>
);
export default withBreadcrumbs(routes)(Breadcrumbs);
For the above example...
Pathname | Result |
---|
/users | Home / Users |
/users/id | Home / Users / John |
/example | Home / Custom Example |
Already using a route config array with react-router?
Just add a breadcrumb
prop to your routes that require custom breadcrumbs.
withBreadcrumbs(routeConfig)(MyComponent)
Dynamic breadcrumbs
If you pass a component as the breadcrumb
prop it will be injected with react-router's match and location objects as props. These objects contain ids, hashes, queries, etc from the route that will allow you to map back to whatever you want to display in the breadcrumb.
Let's use Redux as an example with the match object:
const PureUserBreadcrumb = ({ firstName }) => <span>{firstName}</span>;
const mapStateToProps = (state, props) => ({
firstName: state.userReducer.usersById[props.match.params.id].firstName,
});
export default connect(mapStateToProps)(PureUserBreadcrumb);
Similarly, the location object could be useful for displaying dynamic breadcrumbs based on the route's state:
const EditorBreadcrumb = ({ location: { state: { isNew } } }) => (
<span>{isNew ? 'Add New' : 'Update'}</span>
);
<Link to={{ pathname: '/editor' }}>Edit</Link>
<Link to={{ pathname: '/editor', state: { isNew: true } }}>Add</Link>
Disabling default breadcrumbs for paths
This package will attempt to create breadcrumbs for you based on the route section via humanize-string. For example /users
will auotmatically create the breadcrumb "Users"
. There are two ways to disable default breadcrumbs for a path:
Option 1: Disable all default breadcrumb generation by passing disableDefaults: true
in the options
object
withBreadcrumbs(routes, { disableDefaults: true })
Option 2: Disable individual default breadcrumbs by passing breadcrumb: null
in route config:
{ path: '/a/b', breadcrumb: null }
Option 3: Disable individual default breadcrumbs by passing an excludePaths
array in the options
object
withBreadcrumbs(routes, { excludePaths: ['/', '/no-breadcrumb/for-this-route'] })
Order matters!
Consider the following route configs:
[
{ path: '/users/:id', breadcrumb: 'id-breadcrumb' },
{ path: '/users/create', breadcrumb: 'create-breadcrumb' },
]
To fix the issue above, just adjust the order of your routes:
[
{ path: '/users/create', breadcrumb: 'create-breadcrumb' },
{ path: '/users/:id', breadcrumb: 'id-breadcrumb' },
]
API
Route = {
path: String
breadcrumb: String|Function?
matchOptions?: {
exact?: Boolean,
strict?: Boolean,
}
}
Options = {
excludePaths: Array
disableDefaults: Boolean
}
withBreadcrumbs(routes?: Array<Route>, options? Object<Options>): HigherOrderComponent
getBreadcrumbs({
routes: Array<Route>,
location: Object<Location>,
options: Object<Options>,
}): Array<Breadcrumb>