-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
61 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { RefObject } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useLayoutEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Creates a reference for a prop. This is useful for preserving dependency | ||
* memoization for hooks like useCallback. | ||
* | ||
* @example | ||
* ```js | ||
* // Referencing a simple prop, used in a useCallback function. | ||
* const valueRef = usePropRef(value) | ||
* | ||
* const increment = useCallback(() => { | ||
* const value = valueRef.current | ||
* onChange(value + 1) | ||
* }, [onChange, valueRef]) | ||
* ``` | ||
* | ||
* --- | ||
* | ||
* Multiple props can be passed in using an `object`. | ||
* | ||
* @example | ||
* ```js | ||
* const propRefs = usePropRef({ value, step }) | ||
* | ||
* const increment = useCallback(() => { | ||
* const { value, step } = propRefs.current | ||
* onChange(value + step) | ||
* }, [onChange, propRefs]) | ||
* ``` | ||
* | ||
* @param value The value to reference | ||
* @return The prop reference. | ||
*/ | ||
export function useLatestRef< T >( value: T ): RefObject< T > { | ||
const ref = useRef( value ); | ||
|
||
useLayoutEffect( () => { | ||
ref.current = value; | ||
} ); | ||
|
||
return ref; | ||
} |
This file was deleted.
Oops, something went wrong.