Skip to content

Bug: rules-of-hooks ESLint rule not catching conditional hooks in React.memo() (Typescript) #19117

@zachkirsch

Description

@zachkirsch

I've noticed that the "no conditional hooks" aspect of the rules-of-hooks eslint rule isn't working inside a React.memo()'ed component. The project is typescript and I'm using the parse from @typescript-eslint/parser, so I'm not sure if the problem is with the rule or with the parser.

React version:

Steps To Reproduce

  1. Create a component using React.memo() inside a Typescript project
  2. Add a hook that is called conditionally, e.g.
export const MyMemoizedComponent = React.memo((props: {}) => {
  if (Math.random() > 0.5) {
    React.useEffect(() => {}, []);
  }
  return <div />;
});

Link to code example: https://codesandbox.io/s/no-rules-of-hooks-warning-in-typescript-reactmemo-ei00n?file=/src/index.tsx

The current behavior

ESLint does not show any errors.

The expected behavior

ESLint should show an error like React Hook "React.useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.

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