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

react-ref-composer

v1.0.1

Published

This package provides a simple of combining several [React refs](https://reactjs.org/docs/refs-and-the-dom.html) into a single ref that can passed to a single component or DOM node. See [React issue #13029](https://github.com/facebook/react/issues/13029)

Downloads

2,149

Readme

react-ref-composer

This package provides a simple of combining several React refs into a single ref that can passed to a single component or DOM node. See React issue #13029 for more details.

This is most useful when you want to keep a ref to a node inside your component and also forward it outside using React.forwardRef or pass to a library such as react-beautiful-dnd.

Both React hooks and class components are supported.

Hooks

useRefComposer is a hook for composing refs. Usage example:

import { useRefComposer } from 'react-ref-composer'

export const MyComponent = React.forwardRef((props, outerRef) => {
  const innerRef = useRef()
  const composeRefs = useRefComposer()

  return <div ref={composeRefs(innerRef, outerRef)}>test</div>
})

Here composeRef is a function that you can call with any number of refs. Both object refs and callback refs are supported. The function returns a single "combined" callback ref.

Make sure to always call composeRef with the same number of arguments. In cases when conditional passing of a ref is required you can pass any falsy (undefined, null, 0, "" or false) value instead to temporary "turn off" the ref, e.g.:

... ref={composeRefs(ref1, a && b && ref2)}

Class components

Class components work very similarly, just use createRefComposer instead:

import { createRefComposer } from 'react-ref-composer'

export class MyComponent {
  constructor(props) {
    super(props)
    this.composeRefs = createRefComposer()
  }

  render() {
    return <div ref={this.composeRefs(ref1, ref2, ......)}>test</div>
  }
}

Same rules for composeRef as above apply.

Why another library?

Why create another library? The main problem with existing libraries, including compose-react-refs, is that none of them handle changing only one of the passed refs correctly. Case in point:

function MyComponent(){
  const composeRefs = useRefComposer()
  const ref1 = useCallback(div => console.log('ref1', div), [])
  const ref2 = useCallback(div => console.log('ref2', div), [])
  const ref3 = useCallback(div => console.log('ref3', div), [])
  const [flag, setFlag] = useState(true)

  function onSwitch() {
    console.log('switching')
    setFlag(f => !f)
  }

  return <div ref={composeRefs(ref1, flag ? ref2 : ref3)}>
    <button onClick={onSwitch}>Switch refs</button>
  </div>
}

This is what the expected output looks like when the user clicks the button:

ref1 <div>
ref2 <div>
switching
ref2 null
ref3 <div>

So the old ref resets to null and the new ref is set to the DOM node as expected.

However with compose-react-refs and other similar libraries this happens:

ref1 <div>
ref2 <div>
switching
ref1 null
ref2 null
ref1 <div>
ref3 <div>

Essentially ref1 goes through reset/set cycle. This will trick the consumer of that ref into thinking that the component got unmounted and remounted again, which can be harmful in some cases (e.g. during drag-and-drop operation) and cause undesired behaviour.