Skip to content

Commit b3bb617

Browse files
Fix usePrevious hook JSDoc types (#22597)
1 parent b4b275c commit b3bb617

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

packages/compose/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ _Parameters_
175175

176176
_Returns_
177177

178-
- `T`: The value from the previous render.
178+
- `(T|undefined)`: The value from the previous render.
179179

180180
<a name="useReducedMotion" href="#useReducedMotion">#</a> **useReducedMotion**
181181

packages/compose/src/hooks/use-previous/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import { useEffect, useRef } from '@wordpress/element';
1111
*
1212
* @param {T} value The value to track.
1313
*
14-
* @return {T} The value from the previous render.
14+
* @return {T|undefined} The value from the previous render.
1515
*/
1616
export default function usePrevious( value ) {
17-
const ref = useRef();
17+
// Disable reason: without an explicit type detail, the type of ref will be
18+
// inferred based on the initial useRef argument, which is undefined.
19+
// https://github.com/WordPress/gutenberg/pull/22597#issuecomment-633588366
20+
/* eslint-disable jsdoc/no-undefined-types */
21+
const ref = useRef( /** @type {T|undefined} */ undefined );
22+
/* eslint-enable jsdoc/no-undefined-types */
1823

1924
// Store current value in ref.
2025
useEffect( () => {

0 commit comments

Comments
 (0)