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 🙏

© 2025 – Pkg Stats / Ryan Hefner

core-hooks

v2.0.0

Published

[![Travis build status](http://img.shields.io/travis/jamesplease/core-hooks.svg?style=flat)](https://travis-ci.org/jamesplease/core-hooks) [![npm version](https://img.shields.io/npm/v/core-hooks.svg)](https://www.npmjs.com/package/core-hooks) [![gzip size

Downloads

44

Readme

Core Hooks

Travis build status npm version gzip size

A small collection of commonly-used custom React Hooks.

Motivation

I regularly find myself reusing the same custom hooks in all of my projects, so I abstracted them into a library.

This collection of hooks is intended to remain reasonably sized.

Installation

Install using npm:

npm install core-hooks

or yarn:

yarn add core-hooks

Hooks

useConstant( valueFn )

A hook that guarantees a constant value. Similar to useMemo, except with the guarantee that the cached value will never be purged.

Use useMemo when your application will not break if the value is recomputed. Use useConstant when the value must never change.

import { useConstant } from 'core-hooks';

useConstant(() => createStore());

useOnChange( value, callback, [comparator] )

A hook that calls callback anytime that value changes. callback is called with two arguments: (currentValue, previousValue).

Pass a comparator function to customize the comparison. It is called with two values, currentValue and previousValue. The default comparison is a strict equals (===).

This hook does not return any value.

import { useOnChange } from 'core-hooks';

useOnChange(isVisible, (isVisible, wasVisible) => {
  if (isVisible && !wasVisible) {
    console.log('It just became visible.');
  }
});

usePrevious( value )

This hook returns the previous value of value.

import { usePrevious } from 'core-hooks';

const prevState = usePrevious(state);

Note: if you wish to detect when a value changes, then you may want to consider useOnChange instead.

useIsMounted()

Returns a Boolean representing whether or not the component has mounted.

import { useIsMounted } from 'core-hooks';

const isMounted = useIsMounted();

useLatestRef( value )

Returns an up-to-date ref of value. This is useful when you need to access value in side effect callbacks in your component, such as setTimeout, setInterval, or user input events like key presses.

import { useState } from 'react';
import { useLatestRef } from 'core-hooks';

const [state, setState] = useState();
const stateRef = useLatestRef(state);

useMountTransition( options )

A hook that allows you to create transitions between two states. Common use cases are:

  • General two-state transitions (such as open and close)
  • Animated mounting/unmounting of a single component

The API was designed with both CSS and JS transitions in mind.

options

  • shouldBeMounted: A Boolean representing which state the element is in
  • transitionDurationMs: Optional. How long the transition between the states lasts
  • onEnter: Optional. A callback that is called once the enter transition is complete
  • onLeave: Optional. A callback that is called once the leave transition is complete
  • onEnteringTimeout: Optional. Pass true when using CSS Transitions. This creates a delay between the shouldMount and useActiveClass booleans being flipped to true, so that your mount CSS transition animates properly. If you are not using CSS transitions, then you do not need to pass this option.

The following example demonstrates how you can use this hook for animating a component that is being mounted.

import { useMountTransition } from 'core-hooks';
import classnames from 'classnames';

function MyComponent({ renderChildren }) {
  const [shouldMount, useActiveClass] = useMountTransition({
    shouldBeMounted: renderChildren,
    transitionDurationMs: 500,
    onEnteringTimeout: true,
  });

  return (
    <>
      {shouldMount && (
        <div
          className={classnames('myDiv', {
            'myDiv-active': useActiveClass,
          })}>
          This div animates in and out
        </div>
      )}
    </>
  );
}

Note: This can be considered a Hook replacement of the react-transition-group Transition component, but not for the TransitionGroup component.