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

m-hooks

v1.0.4

Published

A set of reusable react hooks

Downloads

13

Readme

m-hooks

A set of reusable react hooks

NPM JavaScript Style Guide MIT NPM

Install

npm install --save m-hooks

or

yarn add  m-hooks

Hooks

| Name | Arguments | Returns | | -------------------------------------------------------- | ---------------------------------- | ------------------------------------------------------------ | | useFetch | url, options | response, error, loading | | useDebounce | f, time, dependencies | cancel | | useThrottle | f, time, dependencies | cancel | | useClickInside | containerRef, f | | | useClickOutside | containerRef, f | | | useField | initial | { value, set, reset, bind } | | useTitle | title | | | useDidMount | f | - | | useWillUnmount | f | - | | useDidUpdate | f, dependencies | - | | useToggle | initial | { on, set, reset, toggle } | | useHover | - | { hovered, bind } | | useFocus | - | { focused, bind } |

useFetch(url, options?)

import React from 'react'
import { useFetch } from 'm-hooks'

const App = () => {
  const { response, loading, error } = useFetch(
    'https://jsonplaceholder.typicode.com/todos/1'
  )
  return (
    <div>
      <h1>useFetch Usage</h1>
      {loading && <p>加载中...</p>}
      {error && <p>出错了...</p>}
      {response && <p>{response.title}</p>}
    </div>
  )
}

useDebounce(f, time?, deps?)

useThrottle(f, time?, deps?)

import { useDebounce, useThrottle } from 'm-hooks'

const debounceCancel = useDebounce(() => {
  // callback
}, 2000, [a])

const throttleCancel = useThrottle(() => {
  // callback
}, 2000, [a])

useTitle(title)

useTitle('document title')

useField(initial)

import {useField} from 'm-hooks'

const MyComponent = () => {
  const { value, bind } = useField('Type Here...')

  return (
    <div>
      input text:
      {value}
      <input type="text" {...bind} />
    </div>
  )
}

useClickInside(containerRef, f)

Arguments

  • containerRef: the ref of the container element.
  • f: () => void: f is called when click area is inside the contianer element.

useClickOutside(containerRef, f)

Arguments

  • containerRef: the ref of the container element.
  • f: () => void: f is called when click area is outside the contianer element.

useDidMount(f)

Similar to componentDidMount in React class component.

Arguments

  • f: () => void: f is called when component did mount.
useDidMount(() => {
  console.log('componentDidMount')
})

useWillUnmount(f)

Close to the componentWillUnmount in React class component.

Arguments

  • f: () => void: f is called when component will unmount.
useWillUnmount(() => {
  console.log('componentWillUnmount')
})

useDidUpdate(f, deps?)

Similar to componentDidUpdate, it only runs on updates.

Arguments

  • f: () => Function | void: f is called on every updates. Like useEffect, f can return a clean-up function.
  • dependencies?: Array<any>: Optional array for conditionally firing an effect, same as the second argument passed to useEffect.
useDidUpdate(() => {
  console.log('ComponentDidUpdate')
})

useDidUpdate(() => {
  console.log('ComponentDidUpdate')
}, [dep1, dep2])

useToggle(initial)

import { useToggle } from 'm-hooks'

const Toggle = () => {
  const { on, toggle, reset } = useToggle(false)
  return (
    <div>
      {String(on)}
      <button onClick={toggle}>toggle</button>
      <button onClick={reset}>reset</button>
    </div>
  )
}

useHover()

import { useHover } from 'm-hooks'

const Hover = () => {
  const { hovered, bind } = useHover()
  return (
    <div>
      <div {...bind}>
        hovered:
        {String(hovered)}
      </div>
    </div>
  )
}

useFocus()

License

MIT © edwardwang0302


This hook is created using create-react-hook.