# @seznam/compose-react-refs

> A simple utility for composing two or more react refs into a single callback ref.

Latest version **1.0.6** (published 2021-03-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install @seznam/compose-react-refs
pnpm add @seznam/compose-react-refs
yarn add @seznam/compose-react-refs
bun add @seznam/compose-react-refs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2021-03-19 |
| First published | 2019-06-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 114 |
| Author | Martin Jurča |
| Maintainers | mjancarik, jurca |
| Keywords | react, ref, refs, combine, compose, multiple, chain |

## Links

- npm: https://www.npmjs.com/package/@seznam/compose-react-refs
- Repository: https://github.com/seznam/compose-react-refs
- Homepage: https://github.com/seznam/compose-react-refs#readme
- Issues: https://github.com/seznam/compose-react-refs/issues
- npm.io page: https://npm.io/package/@seznam/compose-react-refs

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2021-03-19
- 1.0.5 — 2020-11-26
- 1.0.4 — 2019-11-04
- 1.0.3 — 2019-11-01
- 1.0.2 — 2019-08-02
- 1.0.1 — 2019-06-25
- 1.0.0 — 2019-06-12
- 0.0.0 — 2019-06-12

## README

# Compose react refs

[![Build Status](https://travis-ci.org/seznam/compose-react-refs.svg?branch=master)](https://travis-ci.org/seznam/compose-react-refs)
[![npm](https://img.shields.io/npm/v/@seznam/compose-react-refs.svg)](https://www.npmjs.com/package/@seznam/compose-react-refs)
[![License](https://img.shields.io/npm/l/@seznam/compose-react-refs.svg)](LICENSE)
![npm type definitions](https://img.shields.io/npm/types/@seznam/compose-react-refs.svg)

A simple utility for composing two or more
[react refs](https://reactjs.org/docs/refs-and-the-dom.html) (ref objects and
callbacks are both supported and can be mixed) into a single callback ref. This
enables you to effectively
[set multiple refs on the same component/element](https://github.com/facebook/react/issues/13029).

This utility does not use
[react hooks](https://reactjs.org/docs/hooks-intro.html), therefore it can be
used in class components (and even outside of react world) safely.

## Installation

`compose-react-refs` is available as npm package, you can use `npm` to install
it:

```
npm install --save @seznam/compose-react-refs
```

## Usage

The following example shows usage in a functional component that composes an
external ref with its own ref it uses to focus the renderer `<input>` element:

```typescript jsx
import * as React from 'react'
import composeRefs from '@seznam/compose-react-refs'

export default React.forwardRef((props, externalRef) => {
  const myRef = React.useRef(null)
  
  React.useEffect(() => {
    myRef.current.focus()
  })

  // No need to worry about nulls and undefined refs here, they will be
  // filtered out automatically.
  return <input {...props} ref={composeRefs(myRef, externalRef)}/>
})
```

The `composeRefs` function allows combining any number of refs:

```typescript jsx
import * as React from 'react'
import composeRefs from '@seznam/compose-react-refs'

export default React.forwardRef((props, externalRef) => {
  const myRef = React.useRef(null)
  const otherRef = React.useRef(null)
  return <input {...props} ref={composeRefs(myRef, null, undefined, otherRef, props.extraRef, externalRef)}/>
})
```

The refs will be updated in the order in which they were provided to the
`composeRefs` function. The composed ref passed to react is cached (no need to
use [`useMemo`](https://reactjs.org/docs/hooks-reference.html#usememo) in your
code), improving performance and preventing
[unexpected ref updates](https://reactjs.org/docs/refs-and-the-dom.html#caveats-with-callback-refs).

---
_Source: https://npm.io/package/@seznam/compose-react-refs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
