@seznam/compose-react-refs
Advanced tools
+1
-1
| { | ||
| "name": "@seznam/compose-react-refs", | ||
| "version": "1.0.3", | ||
| "version": "1.0.4", | ||
| "description": "A simple utility for composing two or more react refs into a single callback ref.", | ||
@@ -5,0 +5,0 @@ "main": "composeRefs.js", |
+19
-1
@@ -43,2 +43,4 @@ # Compose react refs | ||
| // No need to worry about nulls and undefined refs here, they will be | ||
| // filtered out automatically. | ||
| return <input {...props} ref={composeRefs(myRef, externalRef)}/> | ||
@@ -48,3 +50,19 @@ }) | ||
| 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. | ||
| `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). |
8006
11.12%67
36.73%