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.