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

ii-react-libraries

v1.3.5

Published

<h1 align="center">ii-react-libraries</h1>

Downloads

9

Readme

A collection of useful react libraries

At the moment, the library contains several react hooks. In future releases it is planned to add useful components and helper functions.

Installation

npm install ii-react-libraries # or yarn add ii-react-libraries

Hooks

useCombinedRefs

const ref = useCombinedRefs(...refs)

Merges refs into one ref. Useful when you need to bind more than one ref to DOM element.

Usage example:

import { useCombinedRefs } from "ii-react-libraries";
import { useDrag, useDrop } from "react-dnd";

function App() {
  const domRef = useRef(null);

  const [, dragRef] = useDrag({
    // ...
  });

  const [, dropRef] = useDrop({
    // ...
  });

  return (
    <div ref={useCombinedRefs(domRef, dragRef, dropRef)}></div>
  );
}

usePrevious

const prevValue = usePrevious(initialValue);

One question that comes up a lot is "When using hooks how do I get the previous value of props or state?". With React class components you have the componentDidUpdate method which receives previous props and state as arguments or you can update an instance variable (this.previous = value) and reference it later to get the previous value. So how can we do this inside a functional component that doesn't have lifecycle methods or an instance to store values on? Hooks to the rescue! We can create a custom hook that uses the useRef hook internally for storing the previous value. See the recipe below with inline comments. You can also find this example in the official React Hooks FAQ.

Usage example:

import { useState, useEffect, useRef } from 'react';
import { usePrevious } from "ii-react-libraries";

// Usage
function App() {
  // State value and setter for our example
  const [count, setCount] = useState(0);
  
  // Get the previous value (was passed into hook on last render)
  const prevCount = usePrevious(count);
  
  // Display both current and previous count value
  return (
    <div>
      <div>Now: {count}, before: {prevCount}</div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
   );
}

usePreviousDifferent

const prevDifferentValue = usePreviousDifferent(initialValue);

Like usePrevious, but returns previous value, different from the current

useShallowEqualSelector

const result = useShallowEqualSelector(selector);

This hook uses useSelector() with shallow equality.

It is shortcut for this code:

import { useSelector, shallowEqual } from "react-redux";

useSelector(selector, shallowEqual);

Usage example:

import { useShallowEqualSelector } from "ii-react-libraries";

useShallowEqualSelector(selector);

useDeepEqualSelector

const result = useDeepEqualSelector(selector[, customizer])

This hook uses useSelector() with deep equality. For comparison function Lodash's _.isEqualWith() is used. You can pass optional customizer parameter which is invoked to compare values. If customizer returns undefined, comparisons are handled by the method instead. The customizer is invoked with up to six arguments: (objValue, othValue [, index|key, object, other, stack]).

Usage example:

import { useDeepEqualSelector } from "ii-react-libraries";

function isGreeting(value) {
  return /^h(?:i|ello)$/.test(value);
}

function customizer(objValue, othValue) {
  if (isGreeting(objValue) && isGreeting(othValue)) {
    return true;
  }
}

useDeepEqualSelector(selector, customizer);