I have implemented a breadcrumb components that forms from the location.pathname. In that case, if a url is localhost:3000/products/13
, the breadcrumbs would display like products > 13
, but I want it to display the name instead of the id, so it should be like products > product_name
In my case I have a table, on clicking a row of a table, instead of appending the name to url, I am pushing a hash value to the url. The resultant page show a breadcrumb containing the hash but I want to display the name instead. What would be the most effective way to do this
Below is my code :
Breadcrumb.js
import {
Breadcrumbs as MUIBreadcrumbs,
Link,
Typography
} from "@material-ui/core";
import { withRouter } from "react-router-dom";
import styled from 'styled-components';
import { fontFamily } from "@material-ui/system";
const Header = styled.p`
font-size: 14px;
font-weight: 600;
color: #211758;
margin-bottom: 0px !important;
`
const BreadcrumbLink = styled(Link)`
font-size: 14px;
font-weight: 600;
color: #211758;
`
const Breadcrumbs = props => {
const {
history,
location: { pathname }
} = props;
const keynames = ['clusters', 'runs']
const pathnames = pathname.split("/").filter(x => x);
return (
<MUIBreadcrumbs separator="›" aria-label="breadcrumb" style = {{fontFamily:'inherit'}}>
{pathnames.map((name, index) => {
const routeTo = `/${pathnames.slice(0, index + 1).join("/")}`;
const isLast = index === pathnames.length - 1;
return isLast? (
pathnames.length === 1 ? <Header key={name}>{name.charAt(0).toUpperCase() + name.slice(1)}</Header> :
<Header key={name}>{name}</Header>
) : (
<BreadcrumbLink key={name} onClick={() => history.push(routeTo)}>
{keynames.includes(name)?name.charAt(0).toUpperCase() + name.slice(1):name}
</BreadcrumbLink>
);
})}
</MUIBreadcrumbs>
);
};
export default withRouter(Breadcrumbs);
The sample code when a table row is clicked :
rowClicked = (params) => {
return (
history.push({
pathname: `/clusters/${params.data.hash_value}/runs`
})
)
}
The resultant breadcrumb in next page is :
clusters > {params.data.hash_value} > runs
(> is the separator in the breadcrumbs)
I want this breadcrumbs instead : clusters > {params.data.name} > runs
How can I achieve this?