Skip to content

Commit

Permalink
UnitControl: fix exhaustive-deps warnings (#44161)
Browse files Browse the repository at this point in the history
* Ignore native eslint warnings

* Add missing dependency on web component

* CHANGELOG

* Memoize `setState` inside `useControlledState` hook
  • Loading branch information
ciampo committed Sep 19, 2022
1 parent cfc881a commit 5d00e5a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
### Internal

- `NavigationMenu` updated to ignore `react/exhaustive-deps` eslint rule ([#44090](https://github.com/WordPress/gutenberg/pull/44090)).
- `UnitControl` updated to pass the `react/exhaustive-deps` eslint rule ([#44161](https://github.com/WordPress/gutenberg/pull/44161)).

## 21.0.0 (2022-09-13)

Expand Down
8 changes: 8 additions & 0 deletions packages/components/src/unit-control/index.native.js
Expand Up @@ -47,6 +47,10 @@ function UnitControl( {
if ( pickerRef?.current ) {
pickerRef.current.presentPicker();
}
// Disable reason: this should be fixed by the native team.
// It would be great if this could be done in the context of
// https://github.com/WordPress/gutenberg/pull/39218
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ pickerRef?.current ] );

const currentInputValue = currentInput === null ? value : currentInput;
Expand Down Expand Up @@ -102,6 +106,10 @@ function UnitControl( {
anchorNodeRef?.current
? findNodeHandle( anchorNodeRef?.current )
: undefined,
// Disable reason: this should be fixed by the native team.
// It would be great if this could be done in the context of
// https://github.com/WordPress/gutenberg/pull/39218
// eslint-disable-next-line react-hooks/exhaustive-deps
[ anchorNodeRef?.current ]
);

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/unit-control/index.tsx
Expand Up @@ -100,7 +100,7 @@ function UnforwardedUnitControl(
if ( parsedUnit !== undefined ) {
setUnit( parsedUnit );
}
}, [ parsedUnit ] );
}, [ parsedUnit, setUnit ] );

// Stores parsed value for hand-off in state reducer.
const refParsedQuantity = useRef< number | undefined >( undefined );
Expand Down
15 changes: 9 additions & 6 deletions packages/components/src/utils/hooks/use-controlled-state.js
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useEffect, useState } from '@wordpress/element';
import { useEffect, useState, useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -70,11 +70,14 @@ function useControlledState( currentState, options = defaultOptions ) {

/* eslint-disable jsdoc/no-undefined-types */
/** @type {(nextState: T) => void} */
const setState = ( nextState ) => {
if ( ! hasCurrentState ) {
setInternalState( nextState );
}
};
const setState = useCallback(
( nextState ) => {
if ( ! hasCurrentState ) {
setInternalState( nextState );
}
},
[ hasCurrentState ]
);
/* eslint-enable jsdoc/no-undefined-types */

return [ state, setState ];
Expand Down

0 comments on commit 5d00e5a

Please sign in to comment.