Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-rules-of-hooks): don't error on hook following non-terminal if statement #1418

Merged
merged 1 commit into from
Feb 19, 2025

Conversation

marvinhagemeister
Copy link
Contributor

@marvinhagemeister marvinhagemeister commented Feb 19, 2025

If an if-statement is placed before a hook it doesn't necessarily mean that the hook was called conditionally. It only is, if the if-statement contains a return statement.

This should error:

function Foo() {
  if (condition) {
    return
  }

  useState(0)
}

...but this is ok:

function Foo() {
  let count = 0;
  if (condition) {
    count++
  }

  useState(0)
}

Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marvinhagemeister marvinhagemeister merged commit 7e361b3 into main Feb 19, 2025
7 checks passed
@marvinhagemeister marvinhagemeister deleted the rules-hooks-fix branch February 19, 2025 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants