Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create react-router v4 breadcrumbs?

Tags:

How do I create react-router v4 breadcrumbs? I tried asking this question on the react-router V4 website via an issue ticket. They just said to see the recursive paths example. I really want to create it in semantic-ui-react

like image 922
Monty Avatar asked Feb 22 '17 00:02

Monty


People also ask

What is breadcrumbs in react?

Breadcrumbs consist of a list of links that help a user visualize a page's location within the hierarchical structure of a website, and allow navigation up to any of its "ancestors".

How do I create a DOM react Router?

How to Install React Router. To install React Router, all you have to do is run npm install react-router-dom@6 in your project terminal and then wait for the installation to complete. If you are using yarn then use this command: yarn add react-router-dom@6 .


2 Answers

I was after the same thing and your question pointed me in the right direction.

This worked for me:

const Breadcrumbs = (props) => (     <div className="breadcrumbs">         <ul className='container'>             <Route path='/:path' component={BreadcrumbsItem} />         </ul>     </div> )  const BreadcrumbsItem = ({ match, ...rest }) => (     <React.Fragment>         <li className={match.isExact ? 'breadcrumb-active' : undefined}>             <Link to={match.url || ''}>                 {match.url}             </Link>         </li>         <Route path={`${match.url}/:path`} component={BreadcrumbsItem} />     </React.Fragment> ) 
like image 87
Felipe Taboada Avatar answered Sep 19 '22 11:09

Felipe Taboada


I used semantic-ui-react for my own project and did this to create breadcrumbs based on location.pathname;

export default (props) => {     const paths = props.pathname.split('/').map((p, i, arr) => {         if (i === 0) return {             key: i,              content: (<Link to={'/'}>home</Link>),              active: (i === arr.length - 1),              link: (i < arr.length - 1)         };          if (i === arr.length - 1) return {             key: i,              content: p,              active: (i === arr.length - 1)         };          return {             key: i,              content: (<Link to={`${arr.slice(0, i + 1).join('/')}`}>{p}</Link>),              active: (i === arr.length - 1),              link: (i < arr.length - 1)}         };     );     return <Breadcrumb icon='chevron right' sections={paths}/>; }; 
like image 44
jimmy Avatar answered Sep 19 '22 11:09

jimmy