Create a breadcrumb list of links with JavaScript and use it inside your React applications!

The advantages of using breadcrumbs in your web applications are widely known.

Suppose we have the following example of chained links in a breadcrumb:

Main page > Main category 1 > Sub category 1 > Product 1

First, lets see how you have to implement the links in the breadcrumb of each page that navigate to the next sub-page. For this we use the JSX element <Link> of React.

The root level of the application will have links to various main categories. Here is an example of a link which navigates to the “main category 1” page:

<Link to="/main-category-1/">Main category 1</Link>

In the “main category 1” page we define the following link that navigates the user to the “sub category 1” page:

<Link to="/main-category-1/sub-category-1/">Sub category 1</Link>

In the “sub category 1” page we define the following link that navigates the user to the “product 1” page:

<Link to="/main-category-1/sub-category-1/product-1/">Sub category 1</Link>

In the “product 1” page we reached the end of the breadcrumb, so there is no link there.

We now want to implement in each page the breadcrumb structure of the current page by writing code in JavaScript. In the previous example we had a small structure of links; imagine though what would happen if we had to show 5 or 10 or 20 links in one breadcrumb. For this reason we want to make our code implementation to be adaptable to the current chain of links. The location object of the window object comes into the rescue.

Following is the code which creates the breadcrumb link structure for our React application. I tried to comment the code so good as possible, so that you understand why we make each step.

    generateBreadCrumb(pathname) {
        var paths = pathname.split("/");
        
        // remove the last element if there was a / at the end of the pathname
        paths = paths[paths.length-1] === "" ? paths.slice(0, paths.length-1): paths;
        
        // remove the first element if the second one is an empty string which means that we are in the root of the website
        paths = paths[1] === "" ? paths.slice(1) : paths;
        
        var breadcrumb = paths.map((path, index) => {
            // Add the > symbol only between two links
            var arrow = index !== paths.length-1 ? " > " : " ";
            
            // The first element should receive the <IndexLink> React element
            if (index === 0) {
                return (<li><IndexLink key={index} to="/" activeClassName="active">Home</IndexLink>{arrow}</li>);
            }
            
            // Build the path for the current URL
            var url = '/' + paths.slice(0, index+1).join('/');
            
            // HTML structure for every link except the first
            return (<li><Link key={index} to={url}>{path}</Link>{arrow}</li>);
        });
        
        // Return a list of links
        return (<ul className="inline-list">{breadcrumb}</ul>);
    }
    
    
    render() {
        // When rendering the component, calculate the HTML structure of the breadcrumb
        var breadcrumb = this.generateBreadCrumb(window.location.pathname);
        
        return (
            <div>
                {breadcrumb}
                // other JSX code
            </div>
        );
    }

The previous code will work also with “border” values of the pathname property:

As an extention, you can define the breadcrumb as an own React component and use it in multiple pages and with other components. This way you impove the re-usability and modularization of your application.

That’s it. I hope that this quick guide helped you create a breadcrumb element in your React application or that you just saw how to make breadcrumb, after all its all plain JavaScript.

comments powered by Disqus