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

@kirekov/great-hooks

v1.7.1

Published

A bunch of useful react-hooks to make your components less verbose and cleaner

Downloads

6

Readme

Great Hooks

A bunch of useful react-hooks to make your components less verbose and cleaner

Table of Contents

Quick Start

Install with npm

npm install @kirekov/great-hooks

Or with yarn

yarn add @kirekov/great-hooks

Status

NPM Build Status Quality Gate Status Coverage JavaScript Style Guide

Usage

:heavy_check_mark: The library has been written in TypeScript. So, it supports types checking.

useAutoUpdateRef

How many times have you written this?

function MyComponent(props) {
  const ref = useRef<string>(props.value);

  useEffect(() => {
    ref.current = props.value;
  }, [props.value]);
  ...
}

Forget about it. That's all you need now.

import { useAutoUpdateRef } from '@kirekov/great-hooks';

function MyComponent(props) {
  const ref = useAutoUpdateRef<string>(props.value);
  ...
}

useEffectOnce

If you need to invoke useEffect only once, you can use that hook.

import { useEffectOnce } from "@kirekov/great-hooks";

function MyComponent(props) {
    useEffectOnce(() => {
        someHeavyOperation();
    })
}

useInterval

If you need to start an interval inside your component, useInterval might be handy.

import { useInterval } from '@kirekov/great-hooks';

function EndlessTimer(props) {
  ...
  const onUpdate = () => { ... };
  useInterval({ callback: onUpdate, interval: 5000 })
}
...
interface UseIntervalParams {
  callback: () => any
  interval: number
  delay?: number
}

delay attribute defines the pause time before the first callback invocation.

If you need to start the interval again, just call the restart function.

const restart = useInterval({ callback: onUpdate, interval: 5000 })
const onRestart = () => {
    restart().then(() => {
        console.log('The interval has been restarted!')
    })
}

useEventListener

Although React provides us with convenient API to bind event listeners to the DOM, sometimes it's required to come up with custom solutions. For example, we can do it like this.

function CustomEventComponent(props) {
  ...
  useEffect(() => {
    document.addEventListener('click', onCustomClick);
    return () => {
      document.removeEventListener('click', onCustomClick);
    };
  }, [])
}

Thankfully great-hooks gives much simpler solution.

import { useEventListener } from '@kirekov/great-hooks';

function CustomEventComponent(props) {
  ...
  useEventListener({ eventName: 'click', onEventTriggered: onCustomClick, eventTarget: document });
}

useStateWithCallback

Have you faced with a problem when you need to execute something exactly after the new state has been saved? It can be easily done with setState approach.

this.setState({ greetings: 'Hi there' }, () => { ... })

So, what about hooks API? great-hooks provides an easy way to do it.

import { useStateWithCallback } from '@kirekov/great-hooks';

function MyComponent(props) {
  const [counter, setCounter] = useStateWithCallback<number>(0);
  function increment() {
    setCounter(prevCounter => prevCounter + 1, newCounterValue => {
      console.log('newCounterValue', newCounterValue);
    })
  }
}

useStateWithPromise

Additionally, the lib provides Promise-like api to handle state updates.

import { useStateWithPromise } from '@kirekov/great-hooks';

function MyComponent(props) {
  const [counter, setCounter] = useStateWithPromise<number>(0);
  function increment() {
    setCounter(prevCounter => prevCounter + 1)
      .then(newCounterValue => {
        console.log('newCounterValue', newCounterValue);
      })
  }
}