Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 16, 2020
1 parent 457db3a commit 2f17186
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,15 @@ _Returns_

<a name="useMergeRefs" href="#useMergeRefs">#</a> **useMergeRefs**

Merges refs.
Merges refs into one ref callback. Ensures the merged ref callbacks are only
called when it changes or when the ref value changes. If you don't wish a ref
callback to be called on every render, wrap it with `useCallback( ref, [] )`.
Dependencies can be added, but when a dependency changes, the ref callback
will be called with the same node.

_Parameters_

- _refs_ `Array`:
- _refs_ `Array<(RefObject|RefCallback)>`: The refs to be merged.

<a name="usePrevious" href="#usePrevious">#</a> **usePrevious**

Expand Down
13 changes: 10 additions & 3 deletions packages/compose/src/hooks/use-merge-refs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
*/
import { useRef, useCallback } from '@wordpress/element';

/** @typedef {import('@wordpress/element').RefObject} RefObject */
/** @typedef {import('@wordpress/element').RefCallback} RefCallback */

/**
* Merges refs.
* Merges refs into one ref callback. Ensures the merged ref callbacks are only
* called when it changes or when the ref value changes. If you don't wish a ref
* callback to be called on every render, wrap it with `useCallback( ref, [] )`.
* Dependencies can be added, but when a dependency changes, the ref callback
* will be called with the same node.
*
* @param {Array} refs
* @param {Array<RefObject|RefCallback>} refs The refs to be merged.
*/
export default function useMergeRefs( refs ) {
const lastValue = useRef( null );
Expand All @@ -24,7 +31,7 @@ export default function useMergeRefs( refs ) {
) {
ref( value );
}
} else if ( ref ) {
} else if ( ref && ref.hasOwnProperty( 'current' ) ) {
ref.current = value;
}
} );
Expand Down

0 comments on commit 2f17186

Please sign in to comment.