Add use-exhaustive-deps to our ESLint rules.
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
useCallbackanduseEffecthooks should be linted to ensure their dependencies are accounted for, souseCallbackanduseEffecthooks do not contain stale references to variables.
Implementation Brief
- Add https://www.npmjs.com/package/eslint-plugin-react-hooks to our
package.jsonand enable it in oureslintrcby adding"react-hooks"to our ESLintpluginsarray and adding"react-hooks/exhaustive-deps": ["error"}]to ourrulesconfiguration. - 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:jsshould pass.
Changelog entry
- N/A
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.
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.
Thanks, @tofumatt! IB :heavy_check_mark:
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 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.
@danielgent Are you ok for me to assign it back to myself?
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 I can pick another ticket if you would like to work on this one. No problem at all 🙂
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 Let's leave it assigned to you and I will work on something else 👍
@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
@tofumatt Note that the IB may need updating as @danielgent points out, enabling this eslint rule generates 110 linting errors.
Interestingly adding "react-hooks/rules-of-hooks": "error" generates no errors!
@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.
QA Eng ✅
- tested
npm run lint - tested
npm run watch - deleting dependancies from
useEffectgenerated warnings as expected