Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-bootstrap breadcrumb with react-router-dom

I'm trying to use react-bootstrap breadcrumb as below.

<Breadcrumb>
    <Breadcrumb.Item href="#">Home</Breadcrumb.Item>
    <Breadcrumb.Item><Link to={"/products"}>Products</Link></Breadcrumb.Item>
    <Breadcrumb.Item active>{productName}</Breadcrumb.Item>
</Breadcrumb>

As you can expect, products Link will render anchor tag inside another anchor tag, which is invalid markup. But Home creates a simple anchor tag instead of react's Link making the page to reload, making it unusable.

What's the solution for this? Unfortunately, there's no mention of this in react-bootstrap doc. (link)

like image 984
Prajwal Avatar asked Mar 02 '18 07:03

Prajwal


People also ask

How do I add breadcrumbs to Reactjs?

There are two main ways to create breadcrumbs in React. js: by using the react-router-dom library or by using the use-react-router-breadcrumbs library. But before starting, you have to create a React app.

How do you change breadcrumbs to dividers?

Dividers. Dividers are automatically added in CSS through ::before and content . They can be changed by modifying a local CSS custom property --bs-breadcrumb-divider , or through the $breadcrumb-divider Sass variable — and $breadcrumb-divider-flipped for its RTL counterpart, if needed.


1 Answers

I would probably use react-router-bootstrap, but if you don't want to include it as a dependency, you can apply the link by hand using the now available linkAs and linkProps Breadcrumb params. For instance:

<Breadcrumb.Item linkAs={Link} linkProps={{ to: "/path" }}>
  My item
</Breadcrumb.Item>

This approach is interesting especially if you are using just the "as" attribute with other components like Button or NavLink.

like image 102
Michel Sabchuk Avatar answered Nov 04 '22 16:11

Michel Sabchuk