Skip to content

Commit

Permalink
Convert usePrevious hook to TypeScript. (#35597)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZebulanStanphill committed Oct 13, 2021
1 parent f6ba963 commit ae12d94
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
31 changes: 0 additions & 31 deletions packages/compose/src/hooks/use-previous/index.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/compose/src/hooks/use-previous/index.ts
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;
}

0 comments on commit ae12d94

Please sign in to comment.