-
Notifications
You must be signed in to change notification settings - Fork 48k
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
let react-hooks/exhaustive-deps additionalHooks have a callback in a different position from 0 #24344
base: main
Are you sure you want to change the base?
Conversation
Comparing: df5d32f...2092c58 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
Would be great to have this merged... |
+1 |
+1 |
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
bump |
I have what I believe to be a very legit use case. My custom hook produces a queryKey and queryData with a "factory" similar to export const useSkippableQueryData = <TAppRoute extends AppRoute>(
_route: TAppRoute,
mainObjectName: ApiModelType,
queryDataFactory: () => SkippableQueryData<TAppRoute>,
dependencies: DependencyList,
): [QueryKey, SkippableQueryData<TAppRoute>] => {
const [queryKey, queryData] = useMemo<
[QueryKey, SkippableQueryData<TAppRoute>]
>(() => {
const queryData = queryDataFactory();
const queryKey: QueryKey = [
mainObjectName,
_route.method,
_route.path,
queryData,
];
return [queryKey, queryData];
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...dependencies, _route, mainObjectName, queryDataFactory]);
return [queryKey, queryData];
}; The important thing is I basically can't refactor this to put the callback in first position, due to how this is used : const [queryKey, queryData] = useSkippableQueryData(
contract.listCompanyExpense,
ApiModel.Expense,
() => ({
// This is typed, thanks to the contract stuff above
params: { companyId: myCompanyId }
}),
[myCompanyId]
) I deadly want my devs not to forget any dependency as it's critical in there; but in such a case it would require to declare |
please merge this pr 🙏 |
+1 please merge this |
Summary
This pull request allows Hooks specified in react-hooks/exhaustive-deps additionalHooks have a callback parameter in a position other than 0.
In my case, I have a hook where the callback is in position 1 rather than 0:
(simplified version, real version handles aborts/errors/etc but not relevant to this change)
I could refactor the hook and put
initialState
after the callback/deps (at pos 2 rather than 0) but I feel like it reads out of order/more difficult to reason about.How did you test this change?
Added valid/invalid tests. Ran tests with
yarn test --watch ESLintRuleExhaustiveDeps
. Existing and new tests pass.