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

react-hookbox

v1.0.2

Published

react-hookbox is a comprehensive utility hook library for React developers, offering a diverse set of essential hooks to streamline and enhance your development process. Simplify complex tasks, handle common functionalities effortlessly, and boost your pr

Downloads

4

Readme

List of hooks

  1. useLocalStorage: A hook for handling data persistence in local storage.
  2. useFormValidation: A hook for managing form validation with customizable rules.
  3. useMediaQuery: A hook for managing responsive design by tracking changes in the viewport size.
  4. useDebounce: A hook for implementing debounce functionality to limit the rate of execution for expensive operations.
  5. useThrottle: A hook for throttling the execution of a function to control how often it can be called.
  6. useAnimation: A hook for managing complex animations, including keyframes and transitions.
  7. useDarkMode: A hook for implementing a dark mode feature in applications.
  8. useClipboard: A hook for handling copying data to the clipboard.

1. useLocalStorage

import {useLocalStorage} from 'react-hooks'

function App() {

 const [count,setCount] = useLocalStorage('count',0)

  return(
    <span>{count}</span>
    <button onClick={() => setCount(count + 1)}>Increment</button>
    </>
  )
}
export default App

2. useFormValidation

import React from "react";
import { useFormValidation } from "react-hooks";

const initialFormState = {
  name: "",
  email: "",
  available: false,
  gender: "",
  password: "",
};

const validationRules = {
  name: (value: string) => value.length > 0,
  email: (value: string) => /\S+@\S+\.\S+/.test(value),
  available: (value: boolean) => value === true,
  gender: (value: string) => value !== "",
  password: (value: string) => value.length >= 6,
};

const App = () => {
  const { formData, errors, handleChange, validateForm } = useFormValidation(
    initialFormState,
    validationRules
  );

  const handleSubmit = (event: React.FormEvent) => {
    event.preventDefault();
    console.log(formData);
    if (validateForm()) {
      // You can perform further actions here, such as submitting the form data.
    }
  };

  return (
    <form onSubmit={handleSubmit}>
      <div>
        <label>Name </label>
        <input
          type="text"
          placeholder="Name"
          value={formData.name}
          onChange={(e) => handleChange("name", e.target.value)}
        />
        {errors.name && <span style={{ color: "red" }}>{errors.name}</span>}
      </div>
      <div>
        <label>Available</label>
        <input
          type="checkbox"
          placeholder="Is available"
          value={formData.available}
          onChange={(e) => handleChange("available", e.target.checked)}
        />
        {errors.name && (
          <span style={{ color: "red" }}>{errors.available}</span>
        )}
      </div>

      <div>
        <label>Gender</label>
        <input
          type="radio"
          placeholder="Is available"
          value="male"
          name="gender"
          onChange={(e) => handleChange("gender", e.target.value)}
        />
        <input
          type="radio"
          placeholder="Is available"
          value="female"
          name="gender"
          onChange={(e) => handleChange("gender", e.target.value)}
        />
        {errors.name && <span style={{ color: "red" }}>{errors.gender}</span>}
      </div>
      <div>
        <label>Email</label>
        <input
          type="text"
          placeholder="Email"
          value={formData.email}
          onChange={(e) => handleChange("email", e.target.value)}
        />
        {errors.email && <span style={{ color: "red" }}>{errors.email}</span>}
      </div>
      <div>
        <label>Password</label>

        <input
          type="password"
          placeholder="Password"
          value={formData.password}
          onChange={(e) => handleChange("password", e.target.value)}
        />
        {errors.password && (
          <span style={{ color: "red" }}>{errors.password}</span>
        )}
      </div>
      <button type="submit">Submit</button>
    </form>
  );
};

export default App;

3. useMediaQuery

import { useMediaQuery } from "react-hooks";

const App = () => {
  const isMobile = useMediaQuery("(max-width: 768px)");

  return (
    <div>
      {isMobile ? (
        <p>Currently displaying on a mobile device.</p>
      ) : (
        <p>Currently displaying on a larger screen.</p>
      )}
    </div>
  );
};

export default App;

4. useDebounce

import { useState } from "react";
import { useDebounce } from "react-hookbox";

const App = () => {
  const [inputValue, setInputValue] = useState("");
  const debouncedValue = useDebounce(inputValue, 500);

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setInputValue(e.target.value);
  };

  return (
    <div>
      <input type="text" value={inputValue} onChange={handleChange} />
      <p>Debounced Value: {debouncedValue}</p>
    </div>
  );
};

export default App;

5. useThrottle

import { useState } from "react";
import { useThrottle } from "react-hookbox";

const App = () => {
  const [count, setCount] = useState(0);
  const throttledCount = useThrottle(count, 1000); // Throttle to 1 second

  return (
    <div>
      <h2>Throttled Counter</h2>
      <p>Actual Count: {count}</p>
      <p>Throttled Count: {throttledCount}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};

export default App;

6. useAnimation

import { useRef } from "react";
import { useAnimation } from "react-hookbox";

const App = () => {
  const boxRef = useRef < HTMLDivElement > null;
  const { startAnimation, playAnimation, pauseAnimation, cancelAnimation } =
    useAnimation();

  const options = {
    keyframes: [
      { transform: "translateX(0)" },
      { transform: "translateX(200px)" },
      { transform: "translateX(0)" },
    ],
    options: {
      duration: 1000,
      iterations: Infinity,
    },
  };

  return (
    <div>
      <div
        ref={boxRef}
        style={{
          width: "100px",
          height: "100px",
          backgroundColor: "red",
        }}
      />
      <button onClick={() => startAnimation(boxRef.current, options)}>
        Start Animation
      </button>
      <button onClick={() => pauseAnimation(boxRef.current)}>
        Pause Animation
      </button>
      <button onClick={() => playAnimation(boxRef.current)}>
        Play Animation
      </button>
      <button onClick={() => cancelAnimation(boxRef.current)}>
        Cancel Animation
      </button>
    </div>
  );
};

export default App;

7. useDarkMode

import { useDarkMode } from "react-hookbox";

const App = () => {
  const [theme, setTheme] = useDarkMode("dark");

  return (
    <div>
      <h1>Current theme = {theme}</h1>
      <button onClick={() => setTheme()}>Change To light</button>
    </div>
  );
};

export default App;

8. useClipboard

import { useClipboard } from "react-hookbox";

const App = () => {
  const [setClipboard, text] = useClipboard();

  return (
    <div>
      <h1>Copied text = {text}</h1>
      <button onClick={() => setClipboard("Text to clipboard")}>
        Copy to clipboard
      </button>
    </div>
  );
};

export default App;