https://github.com/gregberge/react-merge-refs
React utility to merge refs 🖇
https://github.com/gregberge/react-merge-refs
Last synced: 2 months ago
JSON representation
React utility to merge refs 🖇
- Host: GitHub
- URL: https://github.com/gregberge/react-merge-refs
- Owner: gregberge
- License: mit
- Created: 2019-09-19T21:00:57.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2025-05-08T06:09:58.000Z (2 months ago)
- Last Synced: 2025-05-08T07:23:00.132Z (2 months ago)
- Language: TypeScript
- Size: 1.04 MB
- Stars: 627
- Watchers: 4
- Forks: 21
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-list - react-merge-refs
README
# react-merge-refs
[](https://github.com/gregberge/react-merge-refs/blob/main/LICENSE)
[](https://www.npmjs.com/package/react-merge-refs)
[](https://bundlephobia.com/package/react-merge-refs)
[](https://github.com/gregberge/react-merge-refs/actions/workflows/ci.yml)React utility to merge refs 🖇
```sh
npm install react-merge-refs
```## Example
```js
import React from "react";
import { mergeRefs } from "react-merge-refs";const Example = React.forwardRef(function Example(props, ref) {
;
const localRef = React.useRef();
return
});
```## Why?
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.
# License
MIT