
Security News
Bun 1.2.19 Adds Isolated Installs for Better Monorepo Support
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.
react-merge-refs
Advanced tools
The react-merge-refs package allows you to merge multiple React refs into a single ref. This is particularly useful when you need to pass multiple refs to a single DOM element or component.
Merging Multiple Refs
This feature allows you to merge multiple refs into a single ref. In this example, ref1 and ref2 are merged into mergedRef, which is then passed to the div element.
const { useRef } = require('react');
const mergeRefs = require('react-merge-refs');
function MyComponent() {
const ref1 = useRef();
const ref2 = useRef();
const mergedRef = mergeRefs([ref1, ref2]);
return <div ref={mergedRef}>Hello World</div>;
}
React utility to merge refs 🖇
npm install react-merge-refs
import React from "react";
import { mergeRefs } from "react-merge-refs";
const Example = React.forwardRef(function Example(props, ref) {
const localRef = React.useRef();
return <div ref={mergeRefs([localRef, ref])} />;
});
When developing low level UI components, it is common to have to use a local ref but also support an external one using React.forwardRef
. Natively, React does not offer a way to set two refs inside the ref
property. This is the goal of this small utility.
Today a ref
can be a function
or an object
, tomorrow it could be another thing, who knows. This utility handles compatibility for you.
MIT
FAQs
React utility to merge refs.
The npm package react-merge-refs receives a total of 793,383 weekly downloads. As such, react-merge-refs popularity was classified as popular.
We found that react-merge-refs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.
Security News
Popular npm packages like eslint-config-prettier were compromised after a phishing attack stole a maintainer’s token, spreading malicious updates.
Security News
/Research
A phishing attack targeted developers using a typosquatted npm domain (npnjs.com) to steal credentials via fake login pages - watch out for similar scams.