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

moment-hooks

v2.1.0

Published

Incredibly awesome hooks

Downloads

370

Readme

npm version License: MIT

moment-hooks

A module to group all the best hooks togheter.

Prerequisites

  • React 16.8 or greater

Install

yarn install moment-hooks

List of hooks

useOutsideClick

calls a function when a click occurs on the outside of the ref element

Parameters

  1. The ref of the element
  2. The function that is called when a click occurs outside the ref
import { useOutsideClick } from 'moment-hooks';

const ref = React.useRef();
useOutsideClick(ref, () => console.log("On click outside"));

useArray

makes handling of state array easier

Parameters

  1. The initial value of the array
import { useArray } from 'moment-hooks';

const [list, actions] = useArray(["element 1"]);

Return

the hook returns an array with two elements

  1. The array stored in state
  2. The actions you can do on the array

Actions

  1. push - Adds the first parameter of the funtion to the end of the array
  2. removeIndex - Removes the element at the index provided as the first parameter of the function

useLockBodyScroll

Disables the scroll on body. Widely used in modals.

###Parameters

  1. If it should be hidden. Defaults to true.
import { useLockBodyScroll } from 'moment-hooks';

useLockBodyScroll();

or if you have it in a modal

import { useLockBodyScroll } from 'moment-hooks';

const Modal = ({ isOpen }) => {
    useLockBodyScroll(isOpen);

    return <div>Modal</div>;
}

useDebounce

Reduce the amount of time is updated. Useful when dealing with fetch.

Parameters

  1. The value to debounce
  2. The debounce delay in milliseconds. This is the time where no update must ocure to inputValue in order for the debounced value to be updated
import { useDebounce } from 'moment-hooks';

const [inputValue, setInputValue] = React.useState("A value");
const debouncedInputValue = useDebounce(inputValue, 100);

useWindowSize

returns the size of the window

import { useWindowSize } from 'moment-hooks';

const { width, height } = useWindowSize();

useSize

returns the size of the reference

import { useSize } from 'moment-hooks';

const { width, height } = useSize();

Parameters

  1. ref: a React ref

useQueryStringState

Works the same as useState just that it stores it's state in the querystring of the url.

Parameters

  1. defaultState: Exactly the same as the default state of useState
  2. Object with 2 keys:

fromString

A way to serialize the url string to a value in the state

toString

A way to stringify the state.

const [reportSettings, setReportSettings] = useQueryStringState(
        {
            from: moment()
                .subtract(1, 'months')
                .toDate(),
            to: moment().toDate(),
        },
        {
            fromString: {
                from: from => moment(from).toDate(),
                to: to => moment(to).toDate(),
            },
            toString: {
                from: from => moment(from).format(),
                to: to => moment(to).format(),
            },
        }
    );

useWhyDidYouUpdate

logs the reason why a component has been updated

Parameters

  1. The name of the component
  2. The props
import { useWhyDidYouUpdate } from 'moment-hooks';

useWhyDidYouUpdate('Register', props);