ml-in-barcelona / react-rules-of-hooks-ppx

This ppx validates the rules of React hooks.
BSD 2-Clause "Simplified" License
43 stars 1 forks source link

"Error: Hooks can't be inside conditionals, neither loops." with React.useMemo0 hook #8

Open kleinspire opened 3 years ago

kleinspire commented 3 years ago

The following code:

@react.component
let make = () => {
  let className = React.useMemo0(() => "rescript + react + emotion + styled-ppx rocks!")
  <div className />
}

...triggers an error:

3 |   let className = React.useMemo0(() => "rescript + react + emotion + styled-ppx rocks!")
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: Hooks can't be inside conditionals, neither loops.
kleinspire commented 3 years ago

The following seems to work though:

@react.component
let make = () => {
  React.useMemo0(() => "rescript + react + emotion + styled-ppx rocks!")
}
davesnx commented 3 years ago

Hey @webican

Seems like the logic to detect if hooks are being called on the top have a bug where considers the useMemo call to be "inside" the JSX transformation.

I will look into it, since there are a few cases that this linter isn't aligned with what the ESLint plugin does.

Thanks for reporting