Skip to content

[feat] no-direct-set-state-in-use-effect: option to only warn on symmetric set function call with dependency #745

@karlhorky

Description

@karlhorky

Describe the problem

Hi @Rel1cx, thanks for this plugin, amazing!

I'm looking to ban the usage of symmetrical set functions in useEffect without banning set functions entirely:

Only setLimit would be a reported problem (because of the limit in the dependency array), not setItems

import { useEffect, useState } from 'react';
 
function List({ items }) {
  const [items, setItems] = useState([]);
  const [limit, setLimit] = useState(false);
 
  useEffect(() => {
    setLimit(/* ... */);
    setItems(/* ... */);
  }, [limit]);
  // ...
}

Describe the solution you'd like

A new option (default true? would be a breaking change though) to only check for symmetrical setters

Maybe this also affects no-direct-symmetrical-set-state-in-use-layout-effect?

Alternatives considered

A new rule no-direct-symmetrical-set-state-in-use-effect (maybe also no-direct-symmetrical-set-state-in-use-layout-effect?)

Additional context

This would be a less strict rule that would catch a lot of infinite loop patterns without restricting set functions for use cases that can make sense

Use case: teaching students to avoid infinite loops while not being overly strict:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions