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

hooks-me

v0.0.4

Published

<div align="center"> <h1>hooks-me</h1> <div>React useful hooks.</div>

Downloads

460

Readme

npm npm npm npm bundle size

useArray

Easy way to manage array as state. You can pass any Type you want as T.

usage

import { useArray } from "hooks-me";

const Component = () => {
  const { value, push, clear } = useArray<number>(DEFAULT_VALUE);

  return (
    <>
      <div>Value: {value.join(" - ")}</div>
      <button onClick={() => push(7)}>Add 7</button>
      <button onClick={clear}>Clear</button>
    </>
  );
};

API

| name | description | usage | | ------ | --------------------------------------------------------- | ------------------------ | | value | The value as array of T | - | | push | Push a new value to the end of the current array of T | push(7) | | clear | Clear all items. Value will be [] | clear() | | filter | Filter items | filter((id) => id < 5) | | remove | Remove an item with its index | remove(9) | | set | Set the value of array | set([1, 4, 7]) | | update | Replace an item | update(0, 12) |

useEffectOnce

It's simply an upgraded version of the useEffect. You don't have to pass any dependencies as second argument. Only your logic is needed. Voila.

usage

import { useEffectOnce } from "hooks-me";

const Component = () => {
  useEffectOnce(() => {
    doJobOnce();
  });

  return <div>Hooks me, I'm famous.</div>;
};

useToggle

Act like useState to provide:

  • the current value as boolean
  • a setter with one non-mandatory argument

Simple usage:

  • toggleValue(true); -> sets the value to true
  • toggleValue(false); -> sets the value to false
  • toggleValue(); -> sets the value as the opposite of the actual value (t'as compris)

usage

import { useToggle } from "hooks-me";

const Component = () => {
  const [value, toggleValue] = useToggle();

  return (
    <>
      <div>Hooks me, I'm {value ? "very" : "not"} famous.</div>
      <button onClick={() => toggleValue()}>Toggle</button>
      <button onClick={() => toggleValue(true)}>Set true</button>
      <button onClick={() => toggleValue(false)}>Set false</button>
    </>
  );
};

useValidatedState

Pimped version of the useState. It's the same behaviour (current value + setter), but you have to provide a second argument to the hook: the validator.

Validator is just.. a method with only one argument that returns a boolean value. The first argument is of the same type as you defined the hook.

usage

import { useValidatedState } from "hooks-me";

const checkIfValueIsValid = (value: string): boolean => {
  return value !== "famous";
};

const Component = () => {
  const [name, setName, nameIsValid] = useValidatedState<string>(
    "famous",
    checkIfValueIsValid
  );

  return (
    <>
      <div>Hooks me, I'm {name}.</div>
      <div>Is valid: {nameIsValid ? "probably" : "not sure"}</div>
      <button onClick={() => setName("famous")}>Famous</button>
      <button onClick={() => setName("very famous")}>Very famous</button>
    </>
  );
};

useDebounce

Simply add debounce feature for each situation. I ~~guess~~ hope. First thing you'll have to do, is to write you logic as first argument. Second argument is the delay in ms. Last one is the magic one. It's the list of variable which will trigger the debounce stuff. That's all!

usage

import { useState } from "react";
import { useDebounce } from "hooks-me";

const Component = () => {
  const [count, setCount] = useState(0);
  useDebounce(() => alert("Hooks me, bro."), 1000, [count]);

  return (
    <>
      <div>{count}</div>
      <button onClick={() => setCount((old) => old + 1)}>Add</button>
    </>
  );
};

useDebug

Debug your app with this amazing hook. You'll be able to find how many times the wanted component has been updated and see all props that changed during previous rendering.

Let's see following example.

usage

import { useState, type FC } from "react";
import { useDebug } from "hooks-me";

const Component: FC = () => {
  const [count, setCount] = useState(0);

  return (
    <>
      <ChildComponent count={count} />
      <button onClick={() => setCount((old) => old + 1)}>Add</button>
    </>
  );
};

const ChildComponent: FC<{ count: number }> = (props) => {
  const output = useDebug("ChildComponent", props);

  return (
    <>
      <div>{props.count}</div>
      <div>{JSON.stringify(output)}</div>
    </>
  );
};

The output will be printed in your devTools as a console log.

Here's a sample of the output:

{
  "count": 8,
  "changedProps": { "count": { "previous": 5, "current": 6 } },
  "timeSinceLastRender": 200,
  "lastRenderTimestamp": 1666792387890
}

useIsVisible

Get if the given element is visible on screen, or not. The second argument is the possibility to add a positive/negative offset. Perfect for your needs, isn't it?

usage

import { type FC } from "react";
import { useIsVisible } from "hooks-me";

const Component: FC = () => {
  const mainRef = useRef(null);
  const isVisible = useIsVisible(mainRef, "-100px");

  return (
    <>
      <div style={{ height: 2000, backgroundColor: "lightblue" }} />
      <div ref={mainRef}>{isVisible ? "Yep" : "Nope"}</div>
      <div style={{ height: 2000, backgroundColor: "lightblue" }} />
    </>
  );
};

useLocalStorage

usage

import { type FC } from "react";
import { useLocalStorage } from "hooks-me";

const Component: FC = () => {
  const [word, setWord, clearWord] = useLocalStorage("word", "famous");

  return (
    <>
      <div>Hooks me, I'm {word}</div>
      <button onClick={() => setWord("very famous")}>Set another word</button>
      <button onClick={() => clearWord()}>Clear</button>
    </>
  );
};

useSessionStorage

usage

import { type FC } from "react";
import { useSessionStorage } from "hooks-me";

const Component: FC = () => {
  const [word, setWord, clearWord] = useSessionStorage("word", "famous");

  return (
    <>
      <div>Hooks me, I'm {word}</div>
      <button onClick={() => setWord("very famous")}>Set another word</button>
      <button onClick={() => clearWord()}>Clear</button>
    </>
  );
};

useUpdateEffect

Skip the first rendering and trigger the callback after.

usage

import { type FC } from "react";
import { useUpdateEffect } from "hooks-me";

const Component: FC = () => {
  const [count, setCount] = useState(0);

  useUpdateEffect(() => alert("Done!"), [count]);

  return (
    <>
      <div>{count}</div>
      <button onClick={() => setCount((old) => old + 1)}>Add</button>
    </>
  );
};

useWindowSize

Get the window size.

usage

import { type FC } from "react";
import { useWindowSize } from "hooks-me";

const Component: FC = () => {
  const [width, height] = useWindowSize();

  return (
    <>
      <div>Width: {width}</div>
      <div>Height: {height}</div>
    </>
  );
};