admin 管理员组

文章数量: 1086019

Is there an ESLint rule for React to disallow writing useEffect without dependency list?

I'm looking for something like this:

useEffect(() => {
  if (error) handleError(error)
}) // ❌ I expect ESLint complains here, because there's no dependency list
useEffect(() => {
  if (error) handleError(error)
}, [error]) // ✅ Now it has an explicit dependency list

Note that react-hooks/exhaustive-deps doesn't serve that purpose. It complains if the useEffect includes a dependency list but is not exhaustive — for instance, using an empty array ([]) instead of [error], in the second snippet.

本文标签: javascriptESLint rule to disallow useEffect without dependency listStack Overflow