
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-hash-link
Advanced tools
Painless hash link routing for React applications.
The HashLinkObserver
component can be rendered at any level of your component tree and will watch for hash fragments in your URL. When a hash link is encountered, the HashLinkObserver
will scroll to the corresponding element on the page with id="Your-Hash-ID"
.
Contrary to the popular solutions for routing directly to elements on a page in a React application, HashLinkObserver
will work as expected in all of the following scenarios.
✔ Navigating to a URL with a hash fragment and corresponding element on the page
✔ Opening qualifying URLs/pages in a new browser tab or window
✔ Forward browser navigation
✔ Backward browser navigation
✔ Page reload
✔ Works with react-router
but not dependent on it
✔ Works with server-side rendering
✔ All of the above scenarios function correctly when used across all major browsers (including IE)
Navigate to the directory containing your package.json
file
Install react-hash-link
npm install react-hash-link
or
yarn add react-hash-link
Render the HashLinkObserver
component
// ...
import HashLinkObserver from "react-hash-link";
// ...
export const AnyOfMyAppComponents = () => {
// ...
return (
<div>
<AnyOtherComponents />
<HashLinkObserver />
<AnyOtherComponents />
</div>
);
};
// ...
Add an id
to an element on a page.
// ...
export const AnyOfMyComponents = () => {
// ...
return <div id="example">Scroll To Me On Load</div>;
};
// ...
Visit that page and use the element's id
as your hash fragment.
https://my-site.com/example-page#example
You're done, enjoy!
When leveraging SSR, use the AsyncHashLinkObserver
component identically to HashLinkObserver
.
// ...
import {AsyncHashLinkObserver} from "react-hash-link";
// ...
export const AnyOfMyAppComponents = () => {
// ...
return (
<div>
<AnyOtherComponents />
<AsyncHashLinkObserver />
<AnyOtherComponents />
</div>
);
};
// ...
That's it!
HashLinkObserver
/ AsyncHashLinkObserver
PropertiesisPageLoading - An optional boolean value designating whether or not the page is still loading. The HashLinkObserver
won't try to scroll to the target element when the value is true
and will wait until value is false
before attempting.
Note: The isPageLoading
prop can be useful in certain situations such as waiting for an API response to resolve before rendering content on the page. The element you'd like to scroll into view may already be rendered while your data is loading, however, the content rendered as a result of the API call may increase the distance needed to scroll directly to the target element.
smoothScroll - An optional boolean designating whether or not there is a smooth scrolling animation on supported browsers. Defaults to true
.
FAQs
> Painless hash link routing for React applications.
The npm package react-hash-link receives a total of 314 weekly downloads. As such, react-hash-link popularity was classified as not popular.
We found that react-hash-link demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.