Gathering detailed insights and metrics for react-router-breadcrumbs-hoc
Gathering detailed insights and metrics for react-router-breadcrumbs-hoc
Gathering detailed insights and metrics for react-router-breadcrumbs-hoc
Gathering detailed insights and metrics for react-router-breadcrumbs-hoc
npm install react-router-breadcrumbs-hoc
Typescript
Module System
Node Version
NPM Version
58
Supply Chain
94
Quality
76.1
Maintenance
100
Vulnerability
99.6
License
JavaScript (62.31%)
TypeScript (34.35%)
Shell (3.34%)
Total Downloads
4,088,613
Last Day
869
Last Week
17,316
Last Month
88,153
Last Year
759,827
301 Stars
225 Commits
42 Forks
7 Watching
4 Branches
7 Contributors
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
Cumulative downloads
Total Downloads
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
2
37
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.
Render breadcrumbs for react-router
however you want!
yarn add react-router-breadcrumbs-hoc
or
npm i react-router-breadcrumbs-hoc --save
1withBreadcrumbs()(MyComponent);
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);
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 |
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);
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>
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. |
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'] })
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'
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
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
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
38 existing vulnerabilities detected
Details
Score
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