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

[Compiler Bug]: False positive local mutation warning inside filter #31569

Open
1 of 4 tasks
gaearon opened this issue Nov 17, 2024 · 3 comments
Open
1 of 4 tasks

[Compiler Bug]: False positive local mutation warning inside filter #31569

gaearon opened this issue Nov 17, 2024 · 3 comments

Comments

@gaearon
Copy link
Collaborator

gaearon commented Nov 17, 2024

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-compiler (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEASggIZyEBmMEGBA5DGRfQDoB2HCAHjvgZSjsKaCOwIBhWjnYJ2eABTA0eBBjABfAJQFgHAgThiwhE1EqUCAXmLM8AOihgEAWTUQFCnVYB8u-QYEADYIhGhgUhBB1gKkQc4BBkbsJgSQGAgAkqrqMSpqYImB9pRoQaowCnCkTgjWfnrigc1ollU1ziUQEDqNzf0E4ZHRNngwUAhFzRpTgUx4sOJjE7PaHEWtBArpWTlg9iHsAOZ4ABYEfgAMvbPzi7o72QUANIMR3UEzTQZfP68A2vl1ABdLQBO4wcQAHgAJmgAG4EAD0Pg4XxAGiAA

Repro steps

import React from 'react'

export function Component({items}) {
  const stuff = React.useMemo(() => {
    let isCool = false
    const someItems = items
      .filter(cause => {
        if (cause.foo) {
          isCool = true
        }
        return true
      })

    if (someItems.length > 0) {
      return {someItems, isCool}
    }
  }, [items])
  return <div />
}

Getting:

InvalidReact: Reassigning a variable after render has completed can cause inconsistent behavior on subsequent renders. Consider using state instead. Variable `isCool` cannot be reassigned after render (9:9)

But it's not used after render. Curiously, changing the if condition to something that doesn't refer to someItems helps.

How often does this bug happen?

Every time

What version of React are you using?

18.2.0

What version of React Compiler are you using?

19.0.0-beta-a7bf2bd-20241110

@gaearon gaearon added Type: Bug Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Component: Optimizing Compiler labels Nov 17, 2024
@josephsavona josephsavona removed the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Nov 17, 2024
@josephsavona
Copy link
Contributor

Thanks for reporting, definitely a bug.

@ParhamPK03

This comment was marked as spam.

@aalmanasir

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants