site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

Add use-exhaustive-deps to our ESLint rules.

Open tofumatt opened this issue 5 years ago • 15 comments

Feature Description

Further to our decision to use useCallback when defining callback functions in functional components, we should add use-exhaustive-deps to our list of ESLint rules so we're alerted to any useCallback hooks that don't include all of the relevant dependencies in them.

This will affect useEffect code as well, and might highlight existing places where our dependency lists weren't complete.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • All useCallback and useEffect hooks should be linted to ensure their dependencies are accounted for, so useCallback and useEffect hooks do not contain stale references to variables.

Implementation Brief

  • Add https://www.npmjs.com/package/eslint-plugin-react-hooks to our package.json and enable it in our eslintrc by adding "react-hooks" to our ESLint plugins array and adding "react-hooks/exhaustive-deps": ["error"}] to our rules configuration.
  • Fix any existing ESLint errors or document ones that are not legitimate errors (there should be few/none, but it's possible a few useEffects might have legitimate dependency mismatches.

Test Coverage

  • N/A.

Visual Regression Changes

  • N/A.

QA Brief

  • npm run lint:js should pass.

Changelog entry

  • N/A

tofumatt avatar Apr 01 '21 16:04 tofumatt

Add https://www.npmjs.com/package/eslint-plugin-react-hooks to our package.json and enable it in our eslintrc by adding "plugin:react-hooks/recommended" to our "extends" array.

@tofumatt it means that we will add all rules configured by eslint-plugin-react-hooks by default with their default settings. I think it can easily cause side effects and throw unexpected errors that are out of the scope for this ticket. Let's better use custom configuration as it's described in the readme to enable only react-hooks/exhaustive-deps rule from that package to satisfy requirements for this ticket.

eugene-manuilov avatar Apr 12 '21 10:04 eugene-manuilov

Fair enough. The other hooks linting is useful as well and I think worth using, but you're right that it might inflate the scope of the issue. I've adjusted the IB to just worry about exhaustive-deps for now.

tofumatt avatar Apr 12 '21 22:04 tofumatt

Thanks, @tofumatt! IB :heavy_check_mark:

eugene-manuilov avatar Apr 13 '21 07:04 eugene-manuilov

So I'm a bit concerned that the IB says "there should be few/none, but it's possible a few useEffects might have legitimate dependency mismatches."

But running eslint with the new rule gives 110 errors!

The useEffects that have an empty dependency array could be replaced with react-use's useMount (we already have this NPM packages as a dependency and just TourTooltips is using useMount) so I can do this now

But I'm concerned that some of these errors may have their dependency arrays for a reason, and simply adding more dependencies could cause issues

danielgent avatar Apr 19 '21 18:04 danielgent

@danielgent Why did you unassign me from this ticket? I have been working on it for the last couple of hours and already fixed 30 of the 110 errors.

Hazlitte avatar Apr 19 '21 18:04 Hazlitte

@danielgent Are you ok for me to assign it back to myself?

Hazlitte avatar Apr 19 '21 18:04 Hazlitte

Apologies @Hazlitte. I was told to look at it. Leaving it to you now.

I'd be very careful about fixing the errors when it's useEffect as that will change the functionality and probably create really odd bugs.

danielgent avatar Apr 19 '21 18:04 danielgent

@danielgent I can pick another ticket if you would like to work on this one. No problem at all 🙂

Hazlitte avatar Apr 19 '21 18:04 Hazlitte

From what I can see we have

  • useCallback errors: I think we can just do what the linter says. But if we're dealing with third party non-React libraries then sometimes they're funny and hold on to callbacks e.g. ag-grid's React wrapper is a leaky abstraction
  • useEffect with an empty dependency array: can replace this with react-use's useMount
  • useEffect with a non-empty array: IMHO we should just add an eslint-disable comment for this

danielgent avatar Apr 19 '21 18:04 danielgent

@danielgent Let's leave it assigned to you and I will work on something else 👍

Hazlitte avatar Apr 19 '21 18:04 Hazlitte

@Hazlitte I don't mind. Push up your branch if so though and I can continue. Just let me know

I'm a bit concerned though that the IB doesn't expect there to be many errors. So I think just disabling eslint is fine until we discuss more. This eslint-plugin is really needed for hooks development so we do want it installed going forward

Apologies for the unassign. I'm getting used to ZenHub and I thought I'd accidentally assigned it you :facepalm: Will check next time

danielgent avatar Apr 19 '21 18:04 danielgent

@tofumatt Note that the IB may need updating as @danielgent points out, enabling this eslint rule generates 110 linting errors.

Hazlitte avatar Apr 19 '21 18:04 Hazlitte

Interestingly adding "react-hooks/rules-of-hooks": "error" generates no errors!

danielgent avatar Apr 19 '21 18:04 danielgent

@danielgent

  • useCallback errors: I think we can just do what the linter says. But if we're dealing with third party non-React libraries then sometimes they're funny and hold on to callbacks e.g. ag-grid's React wrapper is a leaky abstraction

Let's fix all useCallback errors. We don't use too many non-React libraries as far as I know. So, I think we won't have any problem there.

  • useEffect with an empty dependency array: can replace this with react-use's useMount

Yes, let's replace it with useMount then.

  • useEffect with a non-empty array: IMHO we should just add an eslint-disable comment for this

It also needs to be fixed. That's why we created this ticket.

eugene-manuilov avatar Apr 20 '21 10:04 eugene-manuilov

QA Eng ✅

  • tested npm run lint
  • tested npm run watch
  • deleting dependancies from useEffect generated warnings as expected

Hazlitte avatar Apr 27 '21 15:04 Hazlitte