npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hash-link

v1.0.2

Published

> Painless hash link routing for React applications.

Downloads

3,353

Readme

🖇react-hash-link🖇

Painless hash link routing for React applications.

npm

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)

Getting Started

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!

Server-side Rendering

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!

API Reference

HashLinkObserver / AsyncHashLinkObserver Properties

isPageLoading - 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.