twenty icon indicating copy to clipboard operation
twenty copied to clipboard

[ESLint rule] prevent useRecoilCallback without a dependency array

Open lucasbordeau opened this issue 2 years ago • 3 comments
trafficstars

Scope & Context

We can currently use useRecoilCallback without passing it a dependency array, this is the source of many silent errors.

Technical inputs

We want to enforce the usage of dependency arrays for each useRecoilCallback with a new ESLint rule in our package.

lucasbordeau avatar Nov 17 '23 17:11 lucasbordeau

Hint: Use https://www.npmjs.com/package/eslint-plugin-react-hooks which as exhaustive-deps deps rule (set additionalHooks config)

Also import rules-of-hooks? It seem that eslint-plugin-react-hooks is already in package.json but not in eslint config? Or maybe I missed something

FelixMalfait avatar Jan 25 '24 09:01 FelixMalfait

I would like to work on this

anoopw3bdev avatar Jan 27 '24 09:01 anoopw3bdev

@anoopw3bdev assigned! thanks

FelixMalfait avatar Jan 28 '24 10:01 FelixMalfait

I had a look at this issue, the configurations for custom hooks is already added in the eslint configuration file. Screenshot 2024-02-08 at 10 25 59 PM

I couldn't find anything on the internet that might cause the issue, other than these existing configuration. Appreciate additional inputs and thoughts.

anoopw3bdev avatar Feb 08 '24 17:02 anoopw3bdev

I took a look and it seems that the following piece of code is not detected by the linter:

const deleteSelectedBoardCards = useRecoilCallback(
    ({ snapshot }) =>
      async () => {
        const selectedCardIds = snapshot
          .getLoadable(selectedCardIdsSelector)
          .getValue();

        await deleteManyOpportunities?.(selectedCardIds);
        removeCardIds(selectedCardIds);
        selectedCardIds.forEach((id) => {
          apolloClient.cache.evict({ id: `Opportunity:${id}` });
        });
      },
  );

If you use, the dependencies are checked

const deleteSelectedBoardCards = useRecoilCallback(
    ({ snapshot }) =>
      async () => {
        const selectedCardIds = snapshot
          .getLoadable(selectedCardIdsSelector)
          .getValue();

        await deleteManyOpportunities?.(selectedCardIds);
        removeCardIds(selectedCardIds);
        selectedCardIds.forEach((id) => {
          apolloClient.cache.evict({ id: `Opportunity:${id}` });
        });
      },
    [
      selectedCardIdsSelector,
      deleteManyOpportunities,
      removeCardIds,
      apolloClient.cache,
    ],
  );

@lucasbordeau do you confirm that this is the issue?

charlesBochet avatar Feb 09 '24 13:02 charlesBochet

@charlesBochet Yes !

lucasbordeau avatar Feb 15 '24 09:02 lucasbordeau

Here is the GitStart Ticket for this issue: https://clients.gitstart.com/twenty/5449/tickets/TWNTY-2570

gitstart-app[bot] avatar Mar 05 '24 12:03 gitstart-app[bot]