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

@tzmedical/react-hooks

v3.7.0

Published

Commonly-used custom React hooks for TZ Medical web applications.

Downloads

1,713

Readme

TZ Medical React Hooks

npm version npm downloads CircleCI

This repository contains a collection of custom React hooks.

useFilter

The useFilter hook is designed to filter a list of objects based on a human-inputted text sting. The input can be a word or string to search against all of the preconfigured search fields or a word/string to search against a specific field or a combination of both. The list of fields to search/support is passed into the useFilter hook. See the following table for example interpretations of some example inputs

| User Input | Include all objects for which: | | --------------------- | ----------------------------------------------------------------------------------- | | global | any field contains the word "global" | | -global | no fields contain the word "global" | | multiple words | any field contains the word "multiple" and any field contains the word "words" | | -multiple words | no fields contain the word multiple and any field contains the wor "words" | | "one phrase" | any field contains the string "one phrase" | | -"one phrase" | no fields contain the string "one phrase" | | name:example | the field associated with the name keyword contains the word "example" | | -name:example | the field associated with the name keyword does not contain the word "example" | | name:"include spaces" | the field associated with the name keyword contains the string "include spaces" |

Usage

The following example shows how to configure and use this hook in a component:

import useFilter from "@tzmedical/react-hooks/useFilter"

const filterFields = {
  // searches starting with `name:` search `element.objectNameField` and
  // the field `objectNameField` is also included in keyword-less inputs
  name: "objectNameField",
  // searches starting with `plural:` search both `element.pluralOne` and `element.pluralTwo`
  // the fields `pluralOne` and `pluralTwo` are also included in keyword-less inputs
  plural: ["pluralOne", "pluralTwo"],
  // searches starting with `callback:` will apply the following function to each element, where value is the content after the colon
  callback: (element, value) => complexFilterFunction(element, value),
  // verb keywords like `is` or `has` can be defined as an object listing allowable nouns
  is: {
    // searches for `is:default` will apply the following function to return all elements where `someDefaultFlag` is true
    default: (element) => element.someDefaultFlag
  },
  has: {
    // searches for `has:something` will apply the following function to return all elements where `somethingList` has a length greater than 0
    something: (element) => element.somethingList?.length > 0,
  }
}

function SomeComponent({ originalList, filterText }) {
  const filteredList = useFilter(originalList, filterText, filterFields)

  // do something with filteredList
}

useInterval

Usage

The following example shows how to configure and use this hook in a component:

import React from "react"
import useInterval from "@tzmedical/react-hooks/useInterval"

const INTERVAL_PERIOD_MS = 15 * 1000

function SomeComponent({  }) {
  // Setting manualTrigger to true will cause the periodicFunction to be run immediately
  // Set to true on first render to cause a first run of the function
  // (This state variable is often tied to a component loading state)
  const [manualTrigger, setManualTrigger] = React.useState(true);

  const periodicFunction = React.useCallback(() => {
    // Needs to do something periodically
  })

  useInterval(periodicFunction, INTERVAL_PERIOD_MS, manualTrigger)
}

useSort

Usage

The following example shows how to configure and use this hook in a component:

import useSort from "@tzmedical/react-hooks/useSort"

// By default, on first render sort all elements by the primary field in ascending order,
// Then sort all elements grouped by the primary field according to the secondary field in descending order
const sortOptions = {
  caseSensitive: false,
  defaultSort: [
    { field: 'primary', reverse: false},
    { field: 'secondary', reverse: true},
  ],
}

function SomeComponent({ originalList }) {
  const [sortedList, handleSortCallback, currentSort] = useSort(originalList, sortOptions)

  // Use sortedList to display the data
  // Use currentSort to display the selected sort field and direction
  // Use handleSortCallback to pass the name of a field to sort by. Passing the name of the current sort field reverses direction.
}

useCheckExists

Usage

The following example shows how to configure and use this hook in a component:

import useCheckExists from "@tzmedical/react-hooks/useCheckExists"

// An example list to check against
const list = [
  { name: 'a', value: 1 },
  { name: 'b', value: 2 },
  { name: 'c', value: 3 },
];

function SomeComponent({ myName = 'b' }) {

  const nameExists = useCheckExists(
    list,               // the list of Objects to search
    "name",             // the field to check on each object
    myName,             // a value to skip when checking the list
    "My error message"  // the message to return if a match is NOT found
  );

  // Returns `true`, since an element with the `name` of `a` exists in `list`
  nameExists('a');

  // Returns `My error message`, since no element with the `name` of `z` exists in `list`
  nameExists('z');


  // Returns `My error message`, since elements with the `name` of `b` are skipped in the check
  nameExists('b');
}

useCheckUnique

Usage

The following example shows how to configure and use this hook in a component:

import useCheckUnique from "@tzmedical/react-hooks/useCheckUnique"

// An example list to check against
const list = [
  { name: 'a', value: 1 },
  { name: 'b', value: 2 },
  { name: 'c', value: 3 },
];

function SomeComponent({ myName = 'b' }) {

  const uniqueName = useCheckUnique(
    list,               // the list of Objects to search
    "name",             // the field to check on each object
    myName,             // a value to skip when checking the list
    "My error message"  // the message to return if a match is found
  );

  // Returns `true`, since no element with the `name` of `z` exists in `list`
  uniqueName('z');

  // Returns `My error message`, since an element with the `name` of `a` exists in `list`
  uniqueName('a');

  // Returns `true`, since elements with the `name` of `b` are skipped in the check
  uniqueName('b');
}

useLocalStorage

Usage

The following example shows how to configure and use this hook in a component:

import useLocalStorage from "@tzmedical/react-hooks/useLocalStorage"

function SomeComponent({ myName = 'b' }) {

  const [myState, setMyState] = useLocalStorage("my.state.id", {default: "value"});

  // Use `myState` and `setMyState` just like `React.useState`!
}

Contributing

For local development, an extra step is needed when using npm link to test changes with an external application to prevent multiple versions of React errors:

# Step 1 - link the React package from the application into this package
npm link ../path/to/test/application/node_modules/react

# Step 2 - expose this package for linking
npm link

# Step 3 - link this package into the application
cd ../path/to/test/application
npm link @tzmedical/react-hooks