Design Converter
Education
Software Development Executive - I
Last updated on Aug 21, 2024
Last updated on Nov 20, 2023
Navigating the digital landscapes of modern web applications requires a seamless user experience, especially for single-page applications (SPAs). React Router has established itself as the de facto library for routing in React applications, but it has limitations.
One such limitation is the handling of in-page navigation using hash fragments. That's where the React Router Hash Link library steps in, providing an elegant solution to a common problem.
Imagine a scenario where you want to direct users to a specific section of your SPA—a common requirement for things like FAQ pages or lengthy articles. React Router allows you to switch between components smoothly but doesn't automatically handle scrolling to elements identified by hash fragments in the URL. React Router Hash Link fills this gap, enabling developers to implement hash link scroll functionality with minimal effort.
To begin using React Router Hash Link in your project, you must first ensure that react-router-dom is already part of it, as it's a necessary peer dependency.
1npm install --save react-router-hash-link 2
Once installed, you can incorporate React Router Hash Link components to enhance your application's navigation.
The HashLink component is akin to React Router's <Link>
but with the added benefit of scrolling to the specified hash fragment. Here's an example of how to use HashLink in your React components:
1import { HashLink } from 'react-router-hash-link'; 2 3function App() { 4 return ( 5 <div> 6 <HashLink to="/about#team">Meet the Team</HashLink> 7 </div> 8 ); 9} 10
When you need to highlight the active section, NavHashLink comes into play. It extends the functionality of React Router's <NavLink>
by considering both the path and the hash fragment for applying active styles.
1import { NavHashLink } from 'react-router-hash-link'; 2 3function Navigation() { 4 return ( 5 <nav> 6 <NavHashLink to="/home#intro" activeClassName="active"> 7 Introduction 8 </NavHashLink> 9 </nav> 10 ); 11} 12
The smooth scrolling effect is a subtle yet impactful way to enhance user experience. React Router Hash Link leverages the browser's native scrollIntoView method, providing a smooth transition to the target element.
1function SmoothScrollLink() { 2 return ( 3 <HashLink smooth to="/features#advanced"> 4 Discover Advanced Features 5 </HashLink> 6 ); 7} 8
React Router Hash Link allows a custom scroll function for scenarios where the default scrolling behavior doesn't fit the bill. This allows you to implement complex scrolling logic, such as accounting for fixed headers or other dynamic elements.
1function CustomScrollLink() { 2 return ( 3 <HashLink 4 to="/docs#installation" 5 scroll={(el) => { 6 const yOffset = -100; // Adjust the offset to your liking 7 const y = el.getBoundingClientRect().top + window.pageYOffset + yOffset; 8 window.scrollTo({ top: y, behavior: 'smooth' }); 9 }} 10 > 11 Installation Guide 12 </HashLink> 13 ); 14} 15
There might be instances where the default Link component doesn't meet the specific needs of your project, such as when working with the Gatsby static site generator. React Router Hash Link provides a solution with its genericHashLink function, which allows you to wrap any custom Link component.
1import { genericHashLink } from 'react-router-hash-link'; 2import { Link } from 'gatsby'; 3 4const GatsbyHashLink = genericHashLink(Link); 5 6function CustomLinkComponent() { 7 return ( 8 <GatsbyHashLink to="/blog#comments"> 9 Jump to Comments 10 </GatsbyHashLink> 11 ); 12} 13
React Router Hash Link doesn't just scroll to the target element; it also manages focus according to the native browser's focusing behavior. This ensures that users who rely on keyboard navigation can follow along as the page's focus shifts to the new content.
React Router Hash Link is a powerful addition to the React Router family, addressing the need for in-page navigation using hash fragments. By integrating this library into your React projects, you can provide users with a smooth, intuitive navigation experience that feels natural and responsive.
Embrace the capabilities of React Router Hash Link and watch as your application's navigation transforms from
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.