@stackmeister/react-use-reinitialized-refs
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

@stackmeister/react-use-merged-ref

Got multiple refs from hooks or own useRef calls, but you can only pass one to an element?

This library can merge multiple refs of the same type into one.

Usage

Basic Usage

import useMergedRef from '@stackmeister/react-use-merged-ref'

const App = () => {
  const scrollingRef = useScrolling()
  const { ref: touchRef } = useTouchControls()
  const { calc, ref: calcRef } = useCalc()
  const ref = useMergedRef(scrollingRef, touchRef, calcRef)

  return (
    <div ref={ref}>
      Hello World!
    </div>
  )
}

Easy to encapsulate

import useMergedRef from '@stackmeister/react-use-merged-ref'

const useAppThings = () => {
  const scrollingRef = useScrolling()
  const { ref: touchRef } = useTouchControls()
  const { calc, ref: calcRef } = useCalc()

  return {
    calc,
    ref: useMergedRef(scrollingRef, touchRef, calcRef)
  }
}

const App = () => {
  const { calc, ref } = useAppThings()

  return (
    <div ref={ref}>
      Hello World!
    </div>
  )
}

Readme

Keywords

none

Package Sidebar

Install

npm i @stackmeister/react-use-reinitialized-refs

Weekly Downloads

3

Version

0.0.3

License

MIT

Unpacked Size

5.29 kB

Total Files

7

Last publish

Collaborators

  • torbenkoehn