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

eslint-plugin does not validate dependencies in useSelect and useSuspenseSelect hooks #49897

Closed
ecgan opened this issue Apr 18, 2023 · 1 comment · Fixed by #49900
Closed
Assignees

Comments

@ecgan
Copy link
Contributor

ecgan commented Apr 18, 2023

What problem does this address?

useSelect and useSuspenseSelect hooks in https://www.npmjs.com/package/@wordpress/data have dependencies array as their second parameters. The dependencies array is eventually passed to useCallback in:

const selector = useCallback( mapSelect, deps );

For normal React hooks like useCallback above, we already have 'react-hooks/exhaustive-deps': 'warn' in https://www.npmjs.com/package/@wordpress/eslint-plugin to help us declare all the dependencies.

However, the eslint-plugin does not validate dependencies for our custom useSelect and useSuspenseSelect hooks. This causes issues when developers miss a dependency when using those hooks.

To see this issue in action, find a useSelect usage in the code base, and add or remove an item in its dependencies array. There is no eslint error or warning about the dependencies. Note that you may need to reload the editor window to re-run eslint.

Here's an example: when we remove the clientId in the dependency array in the following useSelect usage, there is no warning or error.

const insertedInNavigationBlock = useSelect(
( select ) => {
const { getBlockParentsByBlockName, wasBlockJustInserted } =
select( blockEditorStore );
return (
!! getBlockParentsByBlockName( clientId, 'core/navigation' )
?.length && wasBlockJustInserted( clientId )
);
},
[ clientId ]
);

image

What is your proposed solution?

Make the eslint-plugin validate all the dependencies in useSelect and useSuspenseSelect hooks.

This can be done by modifying the following line:

'react-hooks/exhaustive-deps': 'warn',

to:

		'react-hooks/exhaustive-deps': [
			'warn',
			{
				additionalHooks: '(useSelect|useSuspenseSelect)',
			},
		],

Reference: https://www.npmjs.com/package/eslint-plugin-react-hooks#advanced-configuration

@ecgan
Copy link
Contributor Author

ecgan commented Apr 18, 2023

I will create a PR with the proposed solution to address this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants