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

typescript-react-hooks-kit

v0.8.2

Published

A collection of reusable, well-documented React hooks written in TypeScript.

Downloads

3

Readme

TypeScript React Hooks Kit

A collection of highly reusable and well-documented custom React hooks written in TypeScript.

About the Project

TypeScript React Hooks Kit is developed and maintained by Adam Peña, under the auspices of Cindersoft, LLC. This project is part of a broader effort to provide high-quality open-source tools and libraries for the React and TypeScript communities.

Installation

npm install typescript-react-hooks-kit

TypeScript Configuration

To use this library effectively, ensure your tsconfig.json is set up correctly. Specifically, the moduleResolution setting should be compatible.

Recommended moduleResolution Setting

While the default moduleResolution is typically sufficient, you may need to use "node16", "nodenext", or "bundler" for projects using ES modules.

{
  "compilerOptions": {
    "moduleResolution": "node16" // or "nodenext" or "bundler"
  }
}

This ensures TypeScript correctly resolves module imports from this library.

Usage

You can import hooks either individually or as named exports from the package.

Example of importing individually:

import useDebounce from 'typescript-react-hooks-kit/useDebounce';

Example of importing as named exports:

import { useDebounce } from 'typescript-react-hooks-kit';

Hooks

useDebounce

Description: Delays updating a value until a specified time has passed without changes.

Usage:

import useDebounce from 'typescript-react-hooks-kit/useDebounce';

const MyComponent = () => {
  const [value, setValue] = useState('');
  const debouncedValue = useDebounce(value, 500);

  return <input value={value} onChange={(e) => setValue(e.target.value)} />;
};

useThrottle

Description: Limits how often a function can be called.

Usage:

import useThrottle from 'typescript-react-hooks-kit/useThrottle';

const MyComponent = () => {
  const [value, setValue] = useState('');
  const throttledValue = useThrottle(value, 1000);

  return <input value={throttledValue} onChange={(e) => setValue(e.target.value)} />;
};

useAsync

Description: Manages an asynchronous operation, handling loading, error, and result states.

Usage:

import useAsync from 'typescript-react-hooks-kit/useAsync';

const MyComponent = () => {
  const { loading, error, value } = useAsync(async () => {
    const response = await fetch('https://api.example.com/data');
    return response.json();
  }, []);

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return <div>Data: {JSON.stringify(value)}</div>;
};

useFetch

Description: Handles fetching data from an API with loading and error states.

Usage:

import useFetch from 'typescript-react-hooks-kit/useFetch';

const MyComponent = () => {
  const { data, loading, error } = useFetch('https://api.example.com/data');

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;

  return <div>Data: {JSON.stringify(data)}</div>;
};

useInterval

Description: Runs a function at specified intervals, like setInterval.

Usage:

import useInterval from 'typescript-react-hooks-kit/useInterval';

const MyComponent = () => {
  const [count, setCount] = useState(0);

  useInterval(() => {
    setCount((prevCount) => prevCount + 1);
  }, 1000);

  return <div>Count: {count}</div>;
};

useLocalStorage

Description: Simplifies working with localStorage in React.

Usage:

import useLocalStorage from 'typescript-react-hooks-kit/useLocalStorage';

const MyComponent = () => {
  const [name, setName] = useLocalStorage('name', 'John Doe');

  return <input value={name} onChange={(e) => setName(e.target.value)} />;
};

useToggle

Description: Provides simple toggle logic for boolean states.

Usage:

import useToggle from 'typescript-react-hooks-kit/useToggle';

const MyComponent = () => {
  const [isOn, toggleIsOn, setIsOn] = useToggle(false);

  return (
    <div>
      <button onClick={toggleIsOn}>Toggle</button>
      <button onClick={() => setIsOn(true)}>Set On</button>
      <div>{isOn ? 'On' : 'Off'}</div>
    </div>
  );
};

usePrevious

Description: Tracks the previous value of a state or prop.

Usage:

import usePrevious from 'typescript-react-hooks-kit/usePrevious';

const MyComponent = ({ value }) => {
  const prevValue = usePrevious(value);

  return <div>Current: {value}, Previous: {prevValue}</div>;
};

useOnClickOutside

Description: Detects clicks outside a specified element and triggers a handler.

Usage:

import useOnClickOutside from 'typescript-react-hooks-kit/useOnClickOutside';

const MyComponent = () => {
  const ref = useRef(null);

  useOnClickOutside(ref, () => {
    console.log('Clicked outside!');
  });

  return <div ref={ref}>Click outside me!</div>;
};

useWindowSize

Description: Tracks the dimensions of the browser window, which is useful for responsive design.

Usage:

import useWindowSize from 'typescript-react-hooks-kit/useWindowSize';

const MyComponent = () => {
  const { width, height } = useWindowSize();

  return (
    <div>
      <p>Window width: {width}px</p>
      <p>Window height: {height}px</p>
    </div>
  );
};

useMediaQuery

Description: Detects whether the viewport matches a given media query.

Usage:

import useMediaQuery from 'typescript-react-hooks-kit/useMediaQuery';

const MyComponent = () => {
  const isLargeScreen = useMediaQuery('(min-width: 1024px)');

  return <div>{isLargeScreen ? 'Large Screen' : 'Small Screen'}</div>;
};

useForm

Description: Manages form state, including handling input changes and form submission.

Usage:

import useForm from 'typescript-react-hooks-kit/useForm';

const MyComponent = () => {
  const { values, handleChange, resetForm } = useForm({ name: '', email: '' });

  return (
    <form>
      <input
        name="name"
        value={values.name}
        onChange={handleChange}
      />
      <input
        name="email"
        value={values.email}
        onChange={handleChange}
      />
      <button type="button" onClick={resetForm}>
        Reset
      </button>
    </form>
  );
};

Contributing

Contributions are welcome! Please open an issue or submit a pull request with your improvements.

License

MIT