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

@wojtekmaj/react-hooks

v1.19.0

Published

A collection of React Hooks.

Downloads

28,970

Readme

npm downloads CI

React-Hooks

A collection of React Hooks.

tl;dr

  • Install by executing npm install @wojtekmaj/react-hooks or yarn add @wojtekmaj/react-hooks.
  • Import by adding import { useTick } from '@wojtekmaj/react-hooks'.
  • Do stuff with it!
    const tick = useTick();

Server-side rendering

All hooks from this package support SSR. Some hooks use browser-only APIs, e.g. useCurrentPosition. Such hooks, if they are supposed to return a value, will return null instead.

User guide

Table of contents

useCurrentPosition

Returns current position from Geolocation API.

Sample usage

import { useCurrentPosition } from '@wojtekmaj/react-hooks';

useCurrentPosition(); // { latitude: 0, longitude: 0 }

useDebouncedEffect

Runs a given effect after a given delay.

Sample usage

import { useDebouncedEffect } from '@wojtekmaj/react-hooks';

useDebouncedEffect(
  () => {
    // This will run after 1 second of value not changing.
  },
  [value],
  1000,
);

useDebouncedState

Returns a debounced state and a function to update it.

Sample usage

import { useDebouncedState } from '@wojtekmaj/react-hooks';

const [value, setValue] = useDebouncedState('initialState', 1000); // ['initialState', Function]

useDebouncedValue

Returns a debounced value.

Sample usage

import { useDebouncedValue } from '@wojtekmaj/react-hooks';

const debouncedValue = useDebouncedValue(value, 1000); // This value will be updated after 1 second of value not changing.

useEventListener

Adds event listener to a given element.

Sample usage

import { useEventListener } from '@wojtekmaj/react-hooks';

useEventListener(element, 'click', onClick);

useIntersectionObserver

Observes a given element using IntersectionObserver.

Sample usage

import { useIntersectionObserver } from '@wojtekmaj/react-hooks';

useIntersectionObserver(element, config, onIntersectionChange);

useLocalStorage

Returns a stateful value synchronized with localStorage, and a function to update it.

Sample usage

import { useLocalStorage } from '@wojtekmaj/react-hooks';

const [value, setValue] = useLocalStorage('myKey', 'initialState'); // ['initialState', Function]

useMatchMedia

Returns a flag which determines if the document matches the given media query string.

Sample usage

import { useMatchMedia } from '@wojtekmaj/react-hooks';

const isDesktop = useMatchMedia('screen and (min-width: 1024px)'); // true / false

useMutationObserver

Observes a given element using MutationObserver.

Sample usage

import { useMutationObserver } from '@wojtekmaj/react-hooks';

useMutationObserver(element, config, onMutation);

useOnLine

Returns the online status of the browser.

Sample usage

import { useOnLine } from '@wojtekmaj/react-hooks';

const onLine = useOnLine(); // true

usePermissionState

Returns permission state given permission name.

Sample usage

import { usePermissionState } from '@wojtekmaj/react-hooks';

const state = usePermissionState({ name: 'geolocation' }); // 'granted'

usePrefersColorSchemeDark

Returns a flag which determines if the document matches (prefers-color-scheme: dark) media feature.

Sample usage

import { usePrefersColorSchemeDark } from '@wojtekmaj/react-hooks';

const prefersColorSchemeDark = usePrefersColorSchemeDark(); // true

usePrefersColorSchemeLight

Returns a flag which determines if the document matches (prefers-color-scheme: light) media feature.

Sample usage

import { usePrefersColorSchemeLight } from '@wojtekmaj/react-hooks';

const prefersColorSchemeLight = usePrefersColorSchemeLight(); // true

usePrefersReducedMotion

Returns a flag which determines if the document matches (prefers-reduced-motion: reduce) media feature.

Sample usage

import { usePrefersReducedMotion } from '@wojtekmaj/react-hooks';

const prefersReducedMotion = usePrefersReducedMotion(); // true

usePrefersReducedTransparency

Returns a flag which determines if the document matches (prefers-reduced-transparency: reduce) media feature.

Sample usage

import { usePrefersReducedTransparency } from '@wojtekmaj/react-hooks';

const prefersReducedTransparency = usePrefersReducedTransparency(); // true

useResizeObserver

Observes a given element using ResizeObserver.

Sample usage

import { useResizeObserver } from '@wojtekmaj/react-hooks';

useResizeObserver(element, config, onResize);

useScrollLeft

Returns current scroll left position in pixels.

Sample usage

import { useScrollLeft } from '@wojtekmaj/react-hooks';

const scrollLeft = useScrollLeft(); // 0

useScrollLeftPercent

Returns current scroll left position in percentage.

Sample usage

import { useScrollLeftPercent } from '@wojtekmaj/react-hooks';

const scrollLeftPercent = useScrollLeftPercent(); // 0.5

useScrollTop

Returns current scroll top position in pixels.

Sample usage

import { useScrollTop } from '@wojtekmaj/react-hooks';

const scrollTop = useScrollTop(); // 0

useScrollTopPercent

Returns current scroll top position in percentage.

Sample usage

import { useScrollTopPercent } from '@wojtekmaj/react-hooks';

const scrollTopPercent = useScrollTopPercent(); // 0.5

useSetInterval

Runs a given function every n milliseconds.

Sample usage

import { useSetInterval } from '@wojtekmaj/react-hooks';

useSetInterval(fn, 1000);

useSetTimeout

Runs a given function after n milliseconds.

Sample usage

import { useSetTimeout } from '@wojtekmaj/react-hooks';

useSetTimeout(fn, 1000);

useTick

Counts from 0, increasing the number returned every n milliseconds.

Sample usage

import { useTick } from '@wojtekmaj/react-hooks';

const tick = useTick(1000); // 0 … 🕐 … 1 … 🕑 … 2 …

useToggle

Returns a flag and a function to toggle it.

Sample usage

import { useToggle } from '@wojtekmaj/react-hooks';

const [value, toggleValue] = useToggle(); // [false, Function]

useWindowHeight

Returns the interior height of the window in pixels.

Sample usage

import { useWindowHeight } from '@wojtekmaj/react-hooks';

const windowWidth = useWindowHeight(); // 900

useWindowWidth

Returns the interior width of the window in pixels.

Sample usage

import { useWindowWidth } from '@wojtekmaj/react-hooks';

const windowWidth = useWindowWidth(); // 1440

License

The MIT License.

Author