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

@hpe/react-hooks

v1.0.4

Published

Reusable react hooks.

Downloads

20

Readme

HPE JS Reusable React Hooks

A set of reusable React hooks to use in your React >= v16.8.0 projects. To install the reusable hooks package run the following command in your project directory. npm install -D @hpe/react-hooks or yarn add @hpe/react-hooks

useFetcher

A set of hooks for fetching asynconchronus data. Uses isomorphic-fetch to allow for server-side rendering. Here's how to use this hook in your React project.

  1. Import the hook at the top of your componenet file import { useFetcher } from '@hpe/react-hooks';
  2. In your component add const [data, loading, error] = useFetcher('https://myapi/data');. When data is available it will return a json parsed object, loading returns a boolean to allow a loading state while error will provide any request errors the hook encountered.

useFetcher accepts a second parameter to accomplish more customized requests, the second parameter behaves exactly a standard fetch call. More information can be found in the fetch spec.

Below is an example React component using the useFetcher hook.

import React from 'react';
import { useFetcher } from '@hpe/react-hooks';

function App() {
  const [data, loading, error] = useFetcher(
    'https://api.openweathermap.org/data/2.5/weather?zip=27278&appid=18ef348ece45174572c5e3d4be8a8d69&units=imperial',
  );
  return (
    <div>
      {loading && <div>Loading...</div>}
      {error && (
        <div>
          This error happened:{' '}
          <span style={{ background: '#d14545', padding: '2px 5px' }}>
            {error.toString()}
          </span>
        </div>
      )}
      {data && (
        <div>
          {data.name} is {data.main.temp} degrees.
        </div>
      )}
    </div>
  );
}

export default App;

useIntersection

intersection example animation

A hook for observing when a DOM node or nodes enters or leaves a browser viewport. useIntersection accepts all parameters which the Intersection Observer API spec accepts including root, rootMargin, and threshold.

Note: this feature is not IE11 compatible without a polyfill

Below is an example React application using the useIntersection hook.

import React from 'react';
import { useIntersection } from '@hpe/react-hooks';

function App() {
  const [thingToWatch, entry] = useIntersection();
  const isVisible = entry.isIntersecting;

  return (
    <div>
      <div style={{ height: '100vh' }} />
      <div ref={thingToWatch}>
        <h1
          style={{
            opacity: isVisible ? 1 : 0,
            transition: 'opacity 1s ease-in',
          }}
        >
          Im now in view{' '}
          <span role="img" aria-label="eyes looking around">
            👀
          </span>
        </h1>
      </div>
      <div style={{ height: '100vh' }} />
    </div>
  );
}

useEntryPosition

intersection example animation

A hook to gain insights on an element's position in the browser viewport. The useEntryPosition returns three array items. The first is your ref to a component in the example we use boxPosToWatch. The second is an object containing elementIs and direction. direction refers to the direction the user is scrolling and will return either up or down. elementIs returns the element's position in relation to the view port, its return strings are leaving, entering, or visible. visible simply means the element is fully contained within the user's viewport. The third item being returned in the callback is entryObserver, this is an intersection observer on your ref. This observer can be used to extend the functionality of the position hook and will allow the developer to be very creative in what is possible with this hook.

Here is an example using the useEntryPosition hook:

import React from 'react';
import { useEntryPosition } from '@hpe/react-hooks';

const Space = () => (
  <div /* The final frontier */ style={{ height: '101vh' }} />
);

function App() {
  const [boxPosToWatch, { direction, elementIs }] = useEntryPosition();
  return (
    <div>
      <Space />
      <div
        ref={boxPosToWatch}
        style={{
          display: 'inline-block',
          padding: '20px',
          border: `${elementIs === 'visible' ? 'green' : 'red'} 3px solid`,
          transition: 'border-color 1s linear',
        }}
      >
        <h1>
          You are scrolling {direction} {direction === 'up' ? '⬆️' : '⬇️️'}{' '}
          while
          <br />
          element is <u>{elementIs}</u> in the window.
        </h1>
      </div>
      <Space />
    </div>
  );
}