Skip to content

Bug: [eslint-plugin-react-hooks] Custom Hooks are not linted with Namespace Imports #18682

@zachhats

Description

@zachhats

Normally if you use a custom hook from anywhere other than a top-level function component, you get a lint error. However, if the custom hook is imported with a namespace import, you don't see a lint error. It's worth mentioning, I'm using typescript, I'm not sure if this repros with regular JS.

React version: We use 16.9, but the create-react-app repro below is with ^16.13.1. It repros with the latest hooks plugin (3.0.0), as well as 2.3.0.

Steps To Reproduce

  1. npx create-react-app namespacerepro --template typescript
  2. Add a new file Hooks.tsx
import React from 'react';

export function useHook(): string {
  const [state] = React.useState("foo");
  return state;
}
  1. Update App.tsx as following:
import React from 'react';
import logo from './logo.svg';
import './App.css';
import * as Hooks from './Hooks';

function App() {
  const text = otherFunction();
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
          {text}
        </a>
      </header>
    </div>
  );
}

function otherFunction(): string {
  return Hooks.useHook();
}

export default App;

The current behavior

No lint error shown.

The expected behavior

Lint error is shown only if changing:

  1. Hook import to import * as Hooks from './Hooks';
  2. Hook call to return useHook();

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