-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert usePrevious hook to TypeScript. (#35597)
- Loading branch information
1 parent
f6ba963
commit ae12d94
Showing
2 changed files
with
24 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Use something's value from the previous render. | ||
* Based on https://usehooks.com/usePrevious/. | ||
* | ||
* @param value The value to track. | ||
* | ||
* @return The value from the previous render. | ||
*/ | ||
export default function usePrevious< T >( value: T ): T | undefined { | ||
const ref = useRef< T >(); | ||
|
||
// Store current value in ref. | ||
useEffect( () => { | ||
ref.current = value; | ||
}, [ value ] ); // Re-run when value changes. | ||
|
||
// Return previous value (happens before update in useEffect above). | ||
return ref.current; | ||
} |