Installations
npm install react-router-breadcrumbs-hoc
Developer Guide
Typescript
Yes
Module System
CommonJS, UMD
Node Version
15.14.0
NPM Version
7.7.6
Score
58
Supply Chain
94
Quality
76.1
Maintenance
100
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (62.31%)
TypeScript (34.35%)
Shell (3.34%)
Developer
Download Statistics
Total Downloads
4,088,613
Last Day
869
Last Week
17,316
Last Month
88,153
Last Year
759,827
GitHub Statistics
301 Stars
225 Commits
42 Forks
7 Watching
4 Branches
7 Contributors
Package Meta Information
Latest Version
4.1.0
Package Id
react-router-breadcrumbs-hoc@4.1.0
Unpacked Size
28.87 kB
Size
7.88 kB
File Count
7
NPM Version
7.7.6
Node Version
15.14.0
Total Downloads
Cumulative downloads
Total Downloads
4,088,613
Last day
-78.3%
869
Compared to previous day
Last week
-23.1%
17,316
Compared to previous week
Last month
9.4%
88,153
Compared to previous month
Last year
-8.3%
759,827
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
2
Dev Dependencies
37
React Router Breadcrumbs HOC
A small (~1.3kb compressed & gzipped), flexible, higher order component for rendering breadcrumbs with react-router 5
example.com/user/123 → Home / User / John Doe
Want to use hooks instead? Try use-react-router-breadcrumbs.
- Description
- Install
- Usage
- Examples
- Route config compatibility
- Dynamic breadcrumbs
- Options
- Order matters!
- API
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
1withBreadcrumbs()(MyComponent);
Examples
Simple
Start seeing generated breadcrumbs right away with this simple example (codesandbox)
1import withBreadcrumbs from 'react-router-breadcrumbs-hoc'; 2 3const Breadcrumbs = ({ breadcrumbs }) => ( 4 <> 5 {breadcrumbs.map(({ breadcrumb }) => breadcrumb)} 6 </> 7) 8 9export 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)
1import withBreadcrumbs from 'react-router-breadcrumbs-hoc'; 2 3const userNamesById = { '1': 'John' } 4 5const DynamicUserBreadcrumb = ({ match }) => ( 6 <span>{userNamesById[match.params.userId]}</span> 7); 8 9// define custom breadcrumbs for certain routes. 10// breadcumbs can be components or strings. 11const routes = [ 12 { path: '/users/:userId', breadcrumb: DynamicUserBreadcrumb }, 13 { path: '/example', breadcrumb: 'Custom Example' }, 14]; 15 16// map, render, and wrap your breadcrumb components however you want. 17const Breadcrumbs = ({ breadcrumbs }) => ( 18 <div> 19 {breadcrumbs.map(({ 20 match, 21 breadcrumb 22 }) => ( 23 <span key={match.url}> 24 <NavLink to={match.url}>{breadcrumb}</NavLink> 25 </span> 26 ))} 27 </div> 28); 29 30export 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...
1const routeConfig = [ 2 { 3 path: "/sandwiches", 4 component: Sandwiches 5 } 6];
becomes...
1const routeConfig = [ 2 { 3 path: "/sandwiches", 4 component: Sandwiches, 5 breadcrumb: 'I love sandwiches' 6 } 7];
then you can just pass the whole route config right into the hook:
1withBreadcrumbs(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:
1// UserBreadcrumb.jsx 2const PureUserBreadcrumb = ({ firstName }) => <span>{firstName}</span>; 3 4// find the user in the store with the `id` from the route 5const mapStateToProps = (state, props) => ({ 6 firstName: state.userReducer.usersById[props.match.params.id].firstName, 7}); 8 9export default connect(mapStateToProps)(PureUserBreadcrumb); 10 11// routes = [{ path: '/users/:id', breadcrumb: UserBreadcrumb }] 12// example.com/users/123 --> Home / Users / John
Now we can pass this custom redux
breadcrumb into the HOC:
1withBreadcrumbs([{
2 path: '/users/:id',
3 breadcrumb: UserBreadcrumb
4}]);
Similarly, the location object could be useful for displaying dynamic breadcrumbs based on the route's state:
1// dynamically update EditorBreadcrumb based on state info 2const EditorBreadcrumb = ({ location: { state: { isNew } } }) => ( 3 <span>{isNew ? 'Add New' : 'Update'}</span> 4); 5 6// routes = [{ path: '/editor', breadcrumb: EditorBreadcrumb }] 7 8// upon navigation, breadcrumb will display: Update 9<Link to={{ pathname: '/editor' }}>Edit</Link> 10 11// upon navigation, breadcrumb will display: Add New 12<Link to={{ pathname: '/editor', state: { isNew: true } }}>Add</Link>
Options
An options object can be passed as the 2nd argument to the hook.
1withBreadcrumbs(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:
1[ 2 { path: '/users/:id', breadcrumb: 'id-breadcrumb' }, 3 { path: '/users/create', breadcrumb: 'create-breadcrumb' }, 4] 5 6// example.com/users/create = 'id-breadcrumb' (because path: '/users/:id' will match first) 7// example.com/users/123 = 'id-breadcumb'
To fix the issue above, just adjust the order of your routes:
1[ 2 { path: '/users/create', breadcrumb: 'create-breadcrumb' }, 3 { path: '/users/:id', breadcrumb: 'id-breadcrumb' }, 4] 5 6// example.com/users/create = 'create-breadcrumb' (because path: '/users/create' will match first) 7// example.com/users/123 = 'id-breadcrumb'
API
1Route = { 2 path: String 3 breadcrumb?: String|Component // if not provided, a default breadcrumb will be returned 4 matchOptions?: { // see: https://reacttraining.com/react-router/web/api/matchPath 5 exact?: Boolean, 6 strict?: Boolean, 7 } 8} 9 10Options = { 11 excludePaths?: string[] // disable default breadcrumb generation for specific paths 12 disableDefaults?: Boolean // disable all default breadcrumb generation 13} 14 15// if routes are not passed, default breadcrumbs will be returned 16withBreadcrumbs(routes?: Route[], options?: Options): HigherOrderComponent 17 18// you shouldn't ever really have to use `getBreadcrumbs`, but it's 19// exported for convenience if you don't want to use the HOC 20getBreadcrumbs({ 21 routes: Route[], 22 options: Options, 23}): Breadcrumb[]
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:40: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/nodejs.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/icd2k3/react-router-breadcrumbs-hoc/nodejs.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 7 third-party GitHubAction dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Reason
38 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-4w2v-q235-vp99
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.5
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More