1.0.11 • Published 3 years ago

react-hooks-utilities v1.0.11

Weekly downloads
32
License
MIT
Repository
github
Last release
3 years ago

Just some react hooks utils... More coming soon (WIP).

Install

npm install --save react-hooks-utilities

Usage

useStateWithCb:

import React from 'react'
import { useStateWithCb } from 'react-hooks-utilities'

function testCallback(value) {
    const el = document.getElementById('test')
    el.innerText = `Current value is ${value}`
}

function Example() {
    const [state, setState] = useStateWithCb(0, testCallback)
    const increment = () => setState((prev) => prev + 1)
    const decrement = () => setState((prev) => prev - 1)
    return (
        <div>
            <button onClick={increment}>Increment</button>
            <button onClick={decrement}>Decrement</button>
            <p id="test"/>
        </div>
    )
}

useDimensions:

import React from 'react'
import { useDimensions } from 'react-hooks-utilities'

function Example() {
    const {
        ref,
        height,
        width
    } = useDimensions()
    return (
        <div ref={ref}>
            <p>Width: {width}px</p>
            <p>Height: {height}px</p>
        </div>
    )
}

useWindowDimensions:

import React from 'react'
import { useWindowDimensions } from 'react-hooks-utilities'

function Example() {
    const {
        height,
        width,
        breakpoint
    } = useWindowDimensions()
    return (
        <div>
            <p>Width: {width}px</p>
            <p>Height: {height}px</p>
            <p>Current breakpoint: {breakpoint}</p>
        </div>
    )
}

useMouseCoordinates:

import React from 'react'
import { useMouseCoordinates } from 'react-hooks-utilities'

function Example() {
    const {
        x,
        y
    } = useMouseCoordinates()
    return (
        <div>
            <p>{x}px</p>
            <p>{y}px</p>
        </div>
    )
}

Or visit this link for live examples.

Todo

  • Move to TypeScript.
  • Write tests.
  • Update examples page.
  • More hooks :sunny:

License

MIT © johnnyboi91

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago