Skip to content

Bug: [eslint-plugin-react-hooks] Constructions should be caught in default props/args #20248

Open
@nstepien

Description

@nstepien

React version: 17.0.1

Steps To Reproduce

  1. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions