0.17.1 • Published 2 months ago

@zimi/hooks v0.17.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@zimi/hooks

install

yarn add  @zimi/hooks

examples

useCombinedRefs
useElementRect
useListen
useRafLoop
useWarnBeforeUnload
useWindowSize


useCombinedRefs

see https://itnext.io/reusing-the-ref-from-forwardref-with-react-hooks-4ce9df693dd

const MeaturedInput = forwardRef<HTMLInputElement>((props, ref) => {
  const innerRef = useCombinedRefs(ref)

  useEffect(() => {
    console.log(innerRef.current?.getBoundingClientRect())
  }, [innerRef])

  return <input ref={innerRef} />
})

↑ all examples ↑

useElementRect

function Test() {
  const ref = useRef<HTMLElement>(null)
  const rect = useElementRect(ref)

  return <div ref={ref}>
    hello world
  </div>
}

// or

// out of component
const element = document.querySelector('xxx')

function Test() {
  const rect = useElementRect(element)
}

↑ all examples ↑

useListen

function Test() {
  const [count, setCount] = useState(0)

  // will be triggered when count changed
  useListen(count, (prev, next) => {
    console.log(prev, next)
  })
}

↑ all examples ↑

useRafLoop

import { useRafLoop } from '@zimi/hooks'

function Test() {
  const now = useRafLoop(() => Date.now())

  return <>{now}</>
}

// or

function Test() {
  const [visibility, setVisibility] = useState(true)

  useRafLoop(() => {
    console.log('rendering...')
  }, {
    enable: visibility,
  })

  return <>
    <p>visibility: {visibility}</p>
    <button onClick={() => {
      setVisibility((prev) => !prev)
    }}>
      toggle visibility
    </button>
  </>
}

↑ all examples ↑

useWarnBeforeUnload

function Test() {
  // show a confirmation dialog before page unload (beforeunload event, NOT component unload)
  // https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
  useWarnBeforeUnload()
}

↑ all examples ↑

useWindowSize

function Test() {
  const innerSize = useWindowSize('inner')
  const outerSize = useWindowSize('outer')
}

↑ all examples ↑

0.17.1

2 months ago

0.16.0

5 months ago

0.14.0

1 year ago

0.15.0

1 year ago

0.10.0

1 year ago

0.11.0

1 year ago

0.8.1

1 year ago

0.12.0

1 year ago

0.8.0

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.16

1 year ago

0.1.14

1 year ago

0.1.10

1 year ago

0.1.9

2 years ago