All Projects → icd2k3 → React Router Breadcrumbs Hoc

icd2k3 / React Router Breadcrumbs Hoc

Licence: mit
tiny, flexible, HOC for rendering route breadcrumbs with react-router v4 & 5

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Router Breadcrumbs Hoc

use-react-router-breadcrumbs
tiny, flexible, hook for rendering route breadcrumbs with react-router v6
Stars: ✭ 170 (-38.41%)
Mutual labels:  react-router, breadcrumbs
Redux Auth Wrapper
A React Higher Order Component (HOC) for handling Authentication and Authorization with Routing and Redux
Stars: ✭ 2,175 (+688.04%)
Mutual labels:  hoc, react-router
covid
COVID-19 updates webpage
Stars: ✭ 117 (-57.61%)
Mutual labels:  react-router
Create React App Material Typescript Redux
A ready to use boilerplate for starting big react projects
Stars: ✭ 257 (-6.88%)
Mutual labels:  react-router
react-pits
React 中的坑
Stars: ✭ 29 (-89.49%)
Mutual labels:  react-router
all-about-reactJS
I'll be working on 20 different ReactJS projects over the course of 60 days and try to create mobile-first, light and dark themed apps out of them.
Stars: ✭ 15 (-94.57%)
Mutual labels:  react-router
universal-routed-flux-demo
The code in this repo is intended for people who want to get started building universal flux applications, with modern and exciting technologies such as Reactjs, React Router and es6.
Stars: ✭ 31 (-88.77%)
Mutual labels:  react-router
awesome-address-book
This project shows a basic address book built with ReactJS, Redux Toolkit and Typescript 📖
Stars: ✭ 20 (-92.75%)
Mutual labels:  react-router
React Music
基于React的在线音乐播放器(移动端高仿安卓网易云音乐)(重构是不可能的,这辈子都不会用 hooks 重构)
Stars: ✭ 269 (-2.54%)
Mutual labels:  react-router
react-admin-dashboard
Building an admin dashboard with React
Stars: ✭ 20 (-92.75%)
Mutual labels:  react-router
Reactgo
Your One-Stop solution for a full-stack universal Redux App!
Stars: ✭ 2,784 (+908.7%)
Mutual labels:  react-router
react-hot-loader-starter-kit
react 16, redux 5, react router 4, universal, webpack 3
Stars: ✭ 41 (-85.14%)
Mutual labels:  react-router
egghead-bookshelf
An example React application to accompany the "Add Internationalization (i18n) to a React app using React Intl" Egghead.io course
Stars: ✭ 28 (-89.86%)
Mutual labels:  react-router
watermark-enhancer
Add watermark to react components in a more elegant way
Stars: ✭ 66 (-76.09%)
Mutual labels:  hoc
breadcrumble
A breadcrumbs plugin for Ruby on Rails.
Stars: ✭ 39 (-85.87%)
Mutual labels:  breadcrumbs
React Native Simple Router
A community maintained router component for React Native
Stars: ✭ 266 (-3.62%)
Mutual labels:  react-router
tmdb-viewer
A movie browsing/favoriting app
Stars: ✭ 63 (-77.17%)
Mutual labels:  react-router
react-template
An enterprise react template application showcasing - Testing strategy, Global state management, middleware support, a network layer, component library integration, localization, PWA support, route configuration, lazy loading and CI/CD
Stars: ✭ 44 (-84.06%)
Mutual labels:  react-router
m2.Breadcrumbs
Magento2. Extension add breadcrumbs to pages in Magento2 that by default do not have breadcrumbs.
Stars: ✭ 17 (-93.84%)
Mutual labels:  breadcrumbs
Shouldcomponentupdate Children
React "Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements 🐇➰
Stars: ✭ 272 (-1.45%)
Mutual labels:  hoc

React Router Breadcrumbs HOC

A small (~1.6kb gzip), flexible, higher order component for rendering breadcrumbs with react-router 4 & 5


example.com/user/123 → Home / User / John Doe


Want to use hooks instead? Try use-react-router-breadcrumbs.

Description

Render breadcrumbs for react-router however you want!

Features

  • Easy to get started with automatically generated breadcrumbs.
  • Render, map, and wrap breadcrumbs any way you want.
  • Compatible with existing route configs.

Install

yarn add react-router-breadcrumbs-hoc

or

npm i react-router-breadcrumbs-hoc --save

Usage

withBreadcrumbs()(MyComponent);

Examples

Simple

Start seeing generated breadcrumbs right away with this simple example (codesandbox)

import withBreadcrumbs from 'react-router-breadcrumbs-hoc';

const Breadcrumbs = ({ breadcrumbs }) => (
  <React.Fragment>
    {breadcrumbs.map(({ breadcrumb }) => breadcrumb)}
  </React.Fragment>
)

export default withBreadcrumbs()(Breadcrumbs);

Advanced

The example above will work for some routes, but you may want other routes to be dynamic (such as a user name breadcrumb). Let's modify it to handle custom-set breadcrumbs. (codesandbox)

import withBreadcrumbs from 'react-router-breadcrumbs-hoc';

const userNamesById = { '1': 'John' }

const DynamicUserBreadcrumb = ({ match }) => (
  <span>{userNamesById[match.params.userId]}</span>
);

// define custom breadcrumbs for certain routes.
// breadcumbs can be components or strings.
const routes = [
  { path: '/users/:userId', breadcrumb: DynamicUserBreadcrumb },
  { path: '/example', breadcrumb: 'Custom Example' },
];

// map, render, and wrap your breadcrumb components however you want.
const Breadcrumbs = ({ breadcrumbs }) => (
  <div>
    {breadcrumbs.map(({
      match,
      breadcrumb
    }) => (
      <span key={match.url}>
        <NavLink to={match.url}>{breadcrumb}</NavLink>
      </span>
    ))}
  </div>
);

export default withBreadcrumbs(routes)(Breadcrumbs);

For the above example...

Pathname Result
/users Home / Users
/users/1 Home / Users / John
/example Home / Custom Example

Route config compatibility

Add breadcrumbs to your existing route config. This is a great way to keep all routing config paths in a single place! If a path ever changes, you'll only have to change it in your main route config rather than maintaining a separate config for react-router-breadcrumbs-hoc.

For example...

const routeConfig = [
  {
    path: "/sandwiches",
    component: Sandwiches
  }
];

becomes...

const routeConfig = [
  {
    path: "/sandwiches",
    component: Sandwiches,
    breadcrumb: 'I love sandwiches'
  }
];

then you can just pass the whole route config right into the hook:

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:

// UserBreadcrumb.jsx
const PureUserBreadcrumb = ({ firstName }) => <span>{firstName}</span>;

// find the user in the store with the `id` from the route
const mapStateToProps = (state, props) => ({
  firstName: state.userReducer.usersById[props.match.params.id].firstName,
});

export default connect(mapStateToProps)(PureUserBreadcrumb);

// routes = [{ path: '/users/:id', breadcrumb: UserBreadcrumb }]
// example.com/users/123 --> Home / Users / John

Now we can pass this custom redux breadcrumb into the HOC:

withBreadcrumbs([{
  path: '/users/:id',
  breadcrumb: UserBreadcrumb
}]);

Similarly, the location object could be useful for displaying dynamic breadcrumbs based on the route's state:

// dynamically update EditorBreadcrumb based on state info
const EditorBreadcrumb = ({ location: { state: { isNew } } }) => (
  <span>{isNew ? 'Add New' : 'Update'}</span>
);

// routes = [{ path: '/editor', breadcrumb: EditorBreadcrumb }]

// upon navigation, breadcrumb will display: Update
<Link to={{ pathname: '/editor' }}>Edit</Link>

// upon navigation, breadcrumb will display: Add New
<Link to={{ pathname: '/editor', state: { isNew: true } }}>Add</Link>

Options

An options object can be passed as the 2nd argument to the hook.

withBreadcrumbs(routes, options)(Component);
Option Type Description
disableDefaults Boolean Disables all default generated breadcrumbs.
excludePaths Array<String> Disables default generated breadcrumbs for specific paths.

Disabling default generated breadcrumbs

This package will attempt to create breadcrumbs for you based on the route section. For example /users will automatically 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' },
]

// example.com/users/create = 'id-breadcrumb' (because path: '/users/:id' will match first)
// example.com/users/123 = 'id-breadcumb'

To fix the issue above, just adjust the order of your routes:

[
  { path: '/users/create', breadcrumb: 'create-breadcrumb' },
  { path: '/users/:id', breadcrumb: 'id-breadcrumb' },
]

// example.com/users/create = 'create-breadcrumb' (because path: '/users/create' will match first)
// example.com/users/123 = 'id-breadcrumb'

API

Route = {
  path: String
  breadcrumb?: String|Component // if not provided, a default breadcrumb will be returned
  matchOptions?: {             // see: https://reacttraining.com/react-router/web/api/matchPath
    exact?: Boolean,
    strict?: Boolean,
  }
}

Options = {
  excludePaths?: string[]       // disable default breadcrumb generation for specific paths
  disableDefaults?: Boolean  // disable all default breadcrumb generation
}

// if routes are not passed, default breadcrumbs will be returned
withBreadcrumbs(routes?: Route[], options?: Options): HigherOrderComponent

// you shouldn't ever really have to use `getBreadcrumbs`, but it's
// exported for convenience if you don't want to use the HOC
getBreadcrumbs({
  routes: Route[],
  options: Options,
}): Breadcrumb[]
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].