Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnitControl: fix exhaustive-deps warnings #44161

Merged
merged 4 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function UnforwardedUnitControl(
if ( parsedUnit !== undefined ) {
setUnit( parsedUnit );
}
}, [ parsedUnit ] );
}, [ parsedUnit, setUnit ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, TIL about this useControlledState thing. Looks useful. I just saw the same pattern in ToggleGroupControl, but without using this utility hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup!

And there's also useControlledValue 🤦 It would be good to see if we can use only one implementation across the whole package


// 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
Original file line number Diff line number Diff line change
@@ -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