ऐसा करने के कई तरीके हैं, और कई खुला स्रोत समाधान (देखें यहाँ उत्तर के साथ-साथ: How do I create react-router v4 breadcrumbs?) कर रहे हैं
व्यक्तिगत रूप से, मैं एक अस्थायी पसंद करते हैं छोटे सतह क्षेत्र की वजह से समाधान, लचीलापन प्रदान करना, और पठनीय ब्रेडक्रंब मार्ग कॉन्फ़िगरेशन।
Breadcrumbs.jsx
import React from 'react';
import { NavLink } from 'react-router-dom';
import { withBreadcrumbs } from 'withBreadcrumbs';
const UserBreadcrumb = ({ match }) =>
<span>{match.params.userId}</span>; // use match param userId to fetch/display user name
const routes = [
{ path: 'users', breadcrumb: 'Users' },
{ path: 'users/:userId', breadcrumb: UserBreadcrumb},
{ path: 'something-else', breadcrumb: ':)' },
];
const Breadcrumbs = ({ breadcrumbs }) => (
<div>
{breadcrumbs.map(({ breadcrumb, path, match }) => (
<span key={path}>
<NavLink to={match.url}>
{breadcrumb}
</NavLink>
<span>/</span>
</span>
))}
</div>
);
export default withBreadcrumbs(routes)(Breadcrumbs);
withBreadcrumbs.js
import React from 'react';
import { matchPath, withRouter } from 'react-router';
const renderer = ({ breadcrumb, match }) => {
if (typeof breadcrumb === 'function') { return breadcrumb({ match }); }
return breadcrumb;
};
export const getBreadcrumbs = ({ routes, pathname }) => {
const matches = [];
pathname
.replace(/\/$/, '')
.split('/')
.reduce((previous, current) => {
const pathSection = `${previous}/${current}`;
let breadcrumbMatch;
routes.some(({ breadcrumb, path }) => {
const match = matchPath(pathSection, { exact: true, path });
if (match) {
breadcrumbMatch = {
breadcrumb: renderer({ breadcrumb, match }),
path,
match,
};
return true;
}
return false;
});
if (breadcrumbMatch) {
matches.push(breadcrumbMatch);
}
return pathSection;
});
return matches;
};
export const withBreadcrumbs = routes => Component => withRouter(props => (
<Component
{...props}
breadcrumbs={
getBreadcrumbs({
pathname: props.location.pathname,
routes,
})
}
/>
));
ओपन-सोर्स हॉक यहां भी उपलब्ध है: https://github.com/icd2k3/react-router-breadcrumbs-hoc
स्रोत
2017-11-05 15:31:22
मैं जानता हूँ कि यह एक पुराने सवाल है, लेकिन प्रतिक्रिया-रूटर के लिए एक घटक है इसका समर्थन करने के लिए, एक नज़र डालें: https://github.com/svenanders/react-breadcrumbs उम्मीद है कि यह – tbo
डी में मदद करता है आईडी आपको इसके लिए समाधान मिल रहा है? – Noitidart