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

useful-custom-react-hooks

v1.0.7

Published

A collection of useful custom React hooks to simplify common tasks and enhance your React applications.

Downloads

21

Readme

Table of Contents

Installing

Using npm:

$ npm i useful-custom-react-hooks

Hooks

useDebounceState

The useDebounceState hook is a custom React hook that provides a debounced version of the useState hook. It allows you to delay the update of a state value until a specified amount of time has passed without any new updates. This can be useful in scenarios where you want to reduce the number of rapid updates and perform some actions only after a certain delay.

Syntax

The useDebounceState hook takes two parameters: value, ms

value (required): The initial value of the state.

ms (optional, default: 200): The debounce delay in milliseconds.

Example

import { useDebounceState } from 'useful-custom-react-hooks'

function App() {
  const [value, setValue] = useDebounceState < string > ('', 1000)

  return (
    <main>
      <h1>{value}</h1>
      <input
        type="text"
        defaultValue={value}
        placeholder="Type here"
        onChange={(e) => setValue(e.target.value)}
      />
    </main>
  )
}
export default App

useStorage

The useStorage hook provides a simplified way to read and write data to the browser's localStorage.

Syntax

useStorage takes two parameters: key and initialState

key (required): The key to use for storing and retrieving data from localStorage.

initialState (optional): The initial state

Example

import { useStorage } from 'useful-custom-react-hooks'

function App() {
  const [data, setData] = useStorage < { message: string } > 'myData'

  const handleSetData = () => {
    setData({
      message: 'hello world',
    })
  }
  const handleDelete = () => {
    // to delete localStorage item just set it to null
    setData(null)
  }

  console.log(data) // {message: 'hello world'}

  return (
    <main>
      <button onClick={handleSetData}>set data</button>
      <button onClick={handleDelete}>delete</button>
      <h1>{data?.message}</h1>
    </main>
  )
}
export default App

useIntersection

The useIntersection hook allows you to track the intersection of a target element with its containing element or the viewport.

Syntax

The useIntersection takes three parameters: root, rootMargin, threshold, once

root (optional): The element that is used as the viewport for checking the target's intersection.

rootMargin (optional): Margin around the root element.

threshold (optional): A number or an array of numbers indicating at what percentage of the target's visibility the observer's callback should be executed.

once (optional, default: false): A boolean indicating whether to observe once

Example

Tailwind used in this example.

import { useIntersection } from 'useful-custom-react-hooks'

function App() {
  const { isIntersecting, ref } = useIntersection({ rootMargin: '-100px' })

  console.log(isIntersecting)

  return (
    <main className="flex flex-col min-h-screen items-center justify-center">
      <div className="h-screen"></div>
      <div ref={ref}>
        {isIntersecting ? 'Intersecting' : 'Not intersecting'}
      </div>
      <div className="h-screen"></div>
    </main>
  )
}
export default App

useFetch

The useFetch hook simplifies making HTTP requests and handling the response asynchronously. It provides the fetched data, error information, and a function to trigger a refetch.

Syntax

The useFetch takes two parameters: url, opts

url (required): The URL to fetch.

opts (optional): An object with additional options for the fetch request.

Example

import { useFetch } from 'useful-custom-react-hooks'

function App() {
  const { data, isFetching, error, refetch } = useFetch(
    'https://jsonplaceholder.typicode.com/todos/1'
  )

  if (isFetching) {
    return <div>Loading...</div>
  }

  if (error) {
    return <div>Error: {error}</div>
  }

  return (
    <main>
      <div>{JSON.stringify(data)}</div>
      <button onClick={() => refetch()}>Refetch</button>
    </main>
  )
}

export default App

useCookie

The useCookie is a hook that simplifies the handling of browser cookies in your React applications.

Syntax

The useCookie takes one parameter: key

key (required): The name of the cookie.

Example

import { useCookie } from 'useful-custom-react-hooks'

function App() {
  const [cookie, setCookie] = useCookie('my_cookie')

  const handleSetCookie = () => {
    // setCookie takes two parameters: value, opts
    setCookie('cookie_value', {
      expires: new Date(Date.now() + 3600000), // Expires in 1 hour
    })
  }

  const handleDeleteCookie = () => {
    // to delete cookie set it to null or set expires in opts to past date
    setCookie(null)
  }

  return (
    <main>
      <button onClick={handleSetCookie}>Set cookie</button>
      <button onClick={handleDeleteCookie}>Delete cookie</button>
      <h1>{cookie}</h1>
    </main>
  )
}

export default App

License

MIT