Skip to content

[feat] no-set-state-directly-in-use-effect #628

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

Closed
SukkaW opened this issue Jul 14, 2024 · 0 comments
Closed

[feat] no-set-state-directly-in-use-effect #628

SukkaW opened this issue Jul 14, 2024 · 0 comments
Labels
Status: Released The issue has been released Type: Feature New features

Comments

@SukkaW
Copy link
Contributor

SukkaW commented Jul 14, 2024

Describe the problem

Common bad practice:

const Comp = () => {
  const [data, setData] = useState();
  useEffect(() => {
    setData();
  }, []);
}

Describe the solution you'd like

Create a rule no-set-state-directly-in-use-effect to prevent set state directly in the top useEffect callback scope:

Invalid

const Comp = () => {
  const [data, setData] = useState();
  useEffect(() => {
    setData();
  }, []);
}

Valid

const Comp = () => {
  const [data, setData] = useState();
  useEffect(() => {
   fetch().then(() => setData());
  }, []);
}
const Comp = () => {
  const [data, setData] = useState();
  useEffect(() => {
   (async () => { setData() })();
  }, []);
}
const Comp = () => {
  const [data, setData] = useState();
  useEffect(() => {
   const onLoad = () => {
     setData();
   };
  }, []);
}

Alternatives considered

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Released The issue has been released Type: Feature New features
Projects
None yet
Development

No branches or pull requests

2 participants