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
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 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 .
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> )
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}/>; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With