@ryvora/react-compose-refs
TypeScript icon, indicating that this package has built-in type declarations

2.0.0Β β€’Β PublicΒ β€’Β Published

Compose Refs πŸ”—

Greetings, Ref Master! πŸ§™β€β™‚οΈ

Ever found yourself in a tricky situation where you need to attach multiple refs to a single React component? πŸ€” The compose-refs module is here to save the day! It elegantly merges multiple refs into a single ref callback that you can pass to your component.

It's like a Voltron for your refs, combining their powers! πŸ€–

Why, though?

Sometimes you have a forwardedRef from a parent, and you also need an internal ref to the same DOM element for measurements or imperative calls. This module makes that clean and easy.

Basic Usage

import { useComposedRefs } from '@ryvora/react-compose-refs';
import React, { useRef } from 'react';

const MyComponent = React.forwardRef((props, forwardedRef) => {
  const internalRef = useRef<HTMLDivElement>(null);
  const composedRefs = useComposedRefs(forwardedRef, internalRef);

  // Now, if forwardedRef exists, it will be updated.
  // And internalRef will also point to the div!

  return <div ref={composedRefs}>Hello!</div>;
});

Ref-tastic! πŸ’« Keep those refs in check!

Package Sidebar

Install

npm i @ryvora/react-compose-refs

Weekly Downloads

4

Version

2.0.0

License

MIT

Unpacked Size

13.6 kB

Total Files

9

Last publish

Collaborators

  • xjectro