question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: [eslint-plugin-react-hooks] RuleOfHooks does not find conditional hook if HOC (e.g. mobx "observer") applied directly

See original GitHub issue

React version: 16.13.1

Steps To Reproduce

  1. Create a component using a conditional hook
  2. Wrap the component with a HOC, e.g. mobx observer

Example

function HOC<T>(c: T): T { return c; }

const ConditionalHookComp = HOC(
    ({ flag }): JSX.Element => {
        if (flag) {
            useEffect(() => console.log("something"));
        }
        return <></>;
    });

The current behavior

The linter does not issue a warning/error for the conditional use of hooks, unless you remove the HOC.

The expected behavior

The linter issues a warning/error for the conditional use of hooks regardless whether the component is wrapped by an HOC or not.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
eps1loncommented, Dec 23, 2020

It’s still kind of weird cause without the HOC wrapper it’s still an unnamed function, just assigned to a variable. So not really consistent in my opinion.

const ConditionalHookComp = () => {}

is not an unnamed function (console.log(ConditionalHookComp.name); // "ConditionalHookComp").

0reactions
ahoislcommented, Dec 23, 2020

You are right, my mistake, name is inferred. We will probably need to follow some pattern for the observer HOC in our project if we want the linter support. Thank you for your support!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why eslint-plugin-react-hooks doesn't warn when using react ...
If I use hooks conditionally in custom hooks, there will be a warning like this: "React Hook \"useState\" is called conditionally. React Hooks...
Read more >
Understanding common frustrations with React Hooks
Let's go over some of these common error messages in React Hooks and discuss why they occur. “React Hook cannot be called inside...
Read more >
README · MobX
The observer wrapper around the TimerView React component will automatically detect that rendering depends on the timer.secondsPassed observable, even though ...
Read more >
mobx-react | Yarn - Package Manager
Package with React component wrapper for combining React with MobX. Exports the observer decorator and other utilities. For documentation, see the MobX ......
Read more >
mobx-react - npm
The simple rule of thumb is: all components that render observable data. If you don't want to mark a component as observer, for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found