Open
Description
React version: 17.0.1
Steps To Reproduce
-
function useMyHook1(arr = []) { useEffect(() => { console.log(arr); }, [arr]); } function useMyHook2({ arr = [] }) { useEffect(() => { console.log(arr); }, [arr]); } function MyComponent({ arr = [] }) { useEffect(() => { console.log(arr); }, [arr]); return null; }
Link to code example: --
The current behavior
No warning appears for arr
being potentially constructed on every render.
The expected behavior
Should warn that arr
can be constructed on each render.
The lint rules already warn for the following, so this is just an extension of the existing behavior:
const arr = props.arr ?? []; // The 'arr' logical expression could make the dependencies of useEffect Hook (at line 54) change on every render. Move it inside the useEffect callback. Alternatively, wrap the initialization of 'arr' in its own useMemo() Hook.
Reference: #19590