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

96jd-react-hooks-utils

v18.0.4

Published

Utility for custom hooks in React

Downloads

121

Readme

96jd-react-hooks-utils

Installation

NPM

npm install 96jd-react-hooks-utils

YARN

yarn add 96jd-react-hooks-utils

PNPM

pnpm add 96jd-react-hooks-utils

BUN

bun add 96jd-react-hooks-utils

Getting started

useArray

import useArray from "96jd-react-hooks-utils/dist/useArray";

export default function App() {
	const { array, set, push, remove, filter, update, clear } = useArray([1, 2, 3, 4, 5, 6]);

	return (
		<div>
			<div>{array.join(", ")}</div>
			<button onClick={() => push(7)}>Add 7</button>
			<button onClick={() => update(1, 9)}>Change Second Element To 9</button>
			<button onClick={() => remove(1)}>Remove Second Element</button>
			<button onClick={() => filter((n) => n < 3)}>Keep Numbers Less Than 4</button>
			<button onClick={() => set([1, 2])}>Set To 1, 2</button>
			<button onClick={clear}>Clear</button>
		</div>
	);
}

useAsync

import useAsync from "96jd-react-hooks-utils/dist/useAsync";

export default function App() {
	const { loading, error, value } = useAsync(
		() =>
			new Promise((resolve, reject) => {
				const success = false;
				setTimeout(() => {
					success ? resolve("Hi") : reject("Error");
				}, 1000);
			})
	);

	return (
		<div>
			<div>Loading: {loading.toString()}</div>
			<div>{error}</div>
			<div>{value}</div>
		</div>
	);
}

useClickOutside

import { useRef, useState } from "react";
import useClickOutside from "96jd-react-hooks-utils/dist/useClickOutside";

export default function App() {
	const [open, setOpen] = useState(false);
	const modalRef = useRef();

	useClickOutside(modalRef, () => open && setOpen(false));

	return (
		<>
			<button onClick={() => setOpen(true)}>Open</button>
			<div
				ref={modalRef}
				style={{
					display: open ? "block" : "none",
					backgroundColor: "blue",
					color: "white",
					width: "100px",
					height: "100px",
					position: "absolute",
					top: "calc(50% - 50px)",
					left: "calc(50% - 50px)"
				}}
			>
				<span>Modal</span>
			</div>
		</>
	);
}

useCookie

import useCookie from "96jd-react-hooks-utils/dist/useCookie";

export default function App() {
	const [value, update, remove] = useCookie("name", "Jacob");

	return (
		<>
			<div>{value}</div>
			<button onClick={() => update("Mohammed")}>Change Name To Mohammed</button>
			<button onClick={remove}>Delete Name</button>
		</>
	);
}

useCopyToClipboard

import useCopyToClipboard from "96jd-react-hooks-utils/dist/useCopyToClipboard";

export default function App() {
	const [copyToClipboard, { success }] = useCopyToClipboard();

	return (
		<>
			<button onClick={() => copyToClipboard("This was copied")}>{success ? "Copied" : "Copy Text"}</button>
			<input type="text" />
		</>
	);
}

useDebounce

import { useState } from "react";
import useDebounce from "96jd-react-hooks-utils/dist/useDebounce";

export default function App() {
	const [count, setCount] = useState(10);
	useDebounce(() => alert(count), 1000, [count]);

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

useDeepCompareEffect

import { useEffect, useState, useRef } from "react";
import useDeepCompareEffect from "96jd-react-hooks-utils/dist/useDeepCompareEffect";

export default function App() {
	const [age, setAge] = useState(0);
	const [otherCount, setOtherCount] = useState(0);
	const useEffectCountRef = useRef();
	const useDeepCompareEffectCountRef = useRef();

	const person = { age: age, name: "Jacob" };

	useEffect(() => {
		useEffectCountRef.current.textContent = parseInt(useEffectCountRef.current.textContent) + 1;
	}, [person]);

	useDeepCompareEffect(() => {
		useDeepCompareEffectCountRef.current.textContent =
			parseInt(useDeepCompareEffectCountRef.current.textContent) + 1;
	}, [person]);

	return (
		<div>
			<div>
				useEffect: <span ref={useEffectCountRef}>0</span>
			</div>
			<div>
				useDeepCompareEffect: <span ref={useDeepCompareEffectCountRef}>0</span>
			</div>
			<div>Other Count: {otherCount}</div>
			<div>{JSON.stringify(person)}</div>
			<button onClick={() => setAge((currentAge) => currentAge + 1)}>Increment Age</button>
			<button onClick={() => setOtherCount((count) => count + 1)}>Increment Other Count</button>
		</div>
	);
}

useEffectOnce

import { useState } from "react";
import useEffectOnce from "96jd-react-hooks-utils/dist/useEffectOnce";

export default function App() {
	const [count, setCount] = useState(0);

	useEffectOnce(() => alert("Hi"));

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

useEventListener

import { useState } from "react";
import useEventListener from "96jd-react-hooks-utils/dist/useEventListener";

export default function App() {
	const [key, setKey] = useState("");
	useEventListener("keydown", (e) => setKey(e.key));

	return <div>Last Key: {key}</div>;
}

useFetch

import { useState } from "react";
import useFetch from "96jd-react-hooks-utils/dist/useFetch";

export default function App() {
	const [id, setId] = useState(1);
	const { loading, error, value } = useFetch(`https://jsonplaceholder.typicode.com/todos/${id}`, {}, [id]);

	return (
		<div>
			<div>{id}</div>
			<button onClick={() => setId((currentId) => currentId + 1)}>Increment ID</button>
			<div>Loading: {loading.toString()}</div>
			<div>{JSON.stringify(error, null, 2)}</div>
			<div>{JSON.stringify(value, null, 2)}</div>
		</div>
	);
}

useGeoLocation

import { useState } from "react";
import useGeoLocation from "96jd-react-hooks-utils/dist/useGeoLocation";

export default function App() {
	const {
		loading,
		error,
		data: { latitude, longitude }
	} = useGeoLocation();

	return (
		<>
			<div>Loading: {loading.toString()}</div>
			<div>Error: {error?.message}</div>
			<div>
				{latitude} x {longitude}
			</div>
		</>
	);
}

useHover

import { useRef } from "react";
import useHover from "96jd-react-hooks-utils/dist/useHover";

export default function App() {
	const elementRef = useRef();
	const hovered = useHover(elementRef);

	return (
		<div
			ref={elementRef}
			style={{
				backgroundColor: hovered ? "blue" : "red",
				width: "100px",
				height: "100px",
				position: "absolute",
				top: "calc(50% - 50px)",
				left: "calc(50% - 50px)"
			}}
		/>
	);
}

useLongPress

import { useRef } from "react";
import useLongPress from "96jd-react-hooks-utils/dist/useLongPress";

export default function App() {
	const elementRef = useRef();
	useLongPress(elementRef, () => alert("Long Press"));

	return (
		<div
			ref={elementRef}
			style={{
				backgroundColor: "red",
				width: "100px",
				height: "100px",
				position: "absolute",
				top: "calc(50% - 50px)",
				left: "calc(50% - 50px)"
			}}
		/>
	);
}

usePrevious

import { useState } from "react";
import usePrevious from "96jd-react-hooks-utils/dist/usePrevious";

export default function App() {
	const [count, setCount] = useState(0);
	const [name, setName] = useState("Jacob");
	const previousCount = usePrevious(count);

	return (
		<div>
			<div>
				{count} - {previousCount}
			</div>
			<div>{name}</div>
			<button onClick={() => setCount((currentCount) => currentCount + 1)}>Increment</button>
			<button onClick={() => setName("Mohammed")}>Change Name</button>
		</div>
	);
}

useRenderCount

import useRenderCount from "96jd-react-hooks-utils/dist/useRenderCount";
import useToggle from "96jd-react-hooks-utils/dist/useToggle";

export default function App() {
	const [boolean, toggle] = useToggle(false);

	const renderCount = useRenderCount();

	return (
		<>
			<div>{boolean.toString()}</div>
			<div>{renderCount}</div>
			<button onClick={toggle}>Toggle</button>
		</>
	);
}

useScript

import useScript from "96jd-react-hooks-utils/dist/useScript";

export default function App() {
	const { loading, error } = useScript("https://code.jquery.com/jquery-3.6.0.min.js");

	if (loading) {
		return <div>Loading</div>;
	}
	if (error) {
		return <div>Error</div>;
	}
	return <div>{window.$(window).width()}</div>;
}

useStateWithHistory

import { useState } from "react";
import useStateWithHistory from "96jd-react-hooks-utils/dist/useStateWithHistory";

export default function App() {
	const [count, setCount, { history, pointer, back, forward, go }] = useStateWithHistory(1);
	const [name, setName] = useState("Jacob");

	return (
		<div>
			<div>{count}</div>
			<div>{history.join(", ")}</div>
			<div>Pointer - {pointer}</div>
			<div>{name}</div>
			<button onClick={() => setCount((currentCount) => currentCount * 2)}>Double</button>
			<button onClick={() => setCount((currentCount) => currentCount + 1)}>Increment</button>
			<button onClick={back}>Back</button>
			<button onClick={forward}>Forward</button>
			<button onClick={() => go(2)}>Go To Index 2</button>
			<button onClick={() => setName("Mohammed")}>Change Name</button>
		</div>
	);
}

useStateWithValidation

import useStateWithValidation from "96jd-react-hooks-utils/dist/useStateWithValidation";

export default function App() {
	const [username, setUsername, isValid] = useStateWithValidation((name) => name.length > 5, "");

	return (
		<>
			<div>Valid: {isValid.toString()}</div>
			<input type="text" value={username} onChange={(e) => setUsername(e.target.value)} />
		</>
	);
}

useLocalStorage

import useLocalStorage from "96jd-react-hooks-utils/dist/useStorage";

export default function App() {
	const [age, setAge, removeAge] = useLocalStorage("age", 28);

	return (
		<div>
			<div>{age}</div>
			<button onClick={() => setAge(40)}>Set Age</button>
			<button onClick={removeAge}>Remove Age</button>
		</div>
	);
}

useSessionStorage

import useSessionStorage from "96jd-react-hooks-utils/dist/useStorage";

export default function App() {
	const [name, setName, removeName] = useSessionStorage("name", "Jacob");

	return (
		<div>
			<div>{name}</div>
			<button onClick={() => setName("John")}>Set Name</button>
			<button onClick={removeName}>Remove Name</button>
		</div>
	);
}

useTimeout

import { useState } from "react";
import useTimeout from "96jd-react-hooks-utils/dist/useTimeout";

export default function App() {
	const [count, setCount] = useState(10);
	const { clear, reset } = useTimeout(() => setCount(0), 1000);

	return (
		<div>
			<div>{count}</div>
			<button onClick={() => setCount((c) => c + 1)}>Increment</button>
			<button onClick={clear}>Clear Timeout</button>
			<button onClick={reset}>Reset Timeout</button>
		</div>
	);
}

useToggle

import useToggle from "96jd-react-hooks-utils/dist/useToggle";

export default function App() {
	const [value, toggleValue] = useToggle(false);

	return (
		<div>
			<div>{value.toString()}</div>
			<button onClick={toggleValue}>Toggle</button>
			<button onClick={() => toggleValue(true)}>Make True</button>
			<button onClick={() => toggleValue(false)}>Make False</button>
		</div>
	);
}

useUpdateEffect

import { useState } from "react";
import useUpdateEffect from "96jd-react-hooks-utils/dist/useUpdateEffect";

export default function App() {
	const [count, setCount] = useState(10);
	useUpdateEffect(() => alert(count), [count]);

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

useWindowSize

import useWindowSize from "96jd-react-hooks-utils/dist/useWindowSize";

export default function App() {
	const { width, height } = useWindowSize();

	return (
		<div>
			{width} x {height}
		</div>
	);
}