Skip to content

[Compiler Bug]: Cannot reassign component props #35319

@otomad

Description

@otomad

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-hooks (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/#N4Igzg9grgTgxgUxALhAgHgBwjALgAgBMEAzAQygBsCSoA7OXASwjvwGEIBbbOhO3AApg+EhAgAafACMyMfAF8AlPmAAdNviYl8ggIRiI+AGTH8e2TBXrN+UePwBefGpCHXAbg12FG7-hgEXFg2QX87AB5CJgA3AD5wu1UAQRgYMgBPADoSGG5hfEp+AHNcAAtkfABmRSlBAH0pJhVHOPwo2PwAawQMx2AmBTjgQwUIgHpo+KVfW3bJ2ITNJS86BRAJEDhWEiZilBAmHhwCXAzMBFV8AAVKKGKmOgB5TGZWMEVRPK58AHJZaQISgAWkwdwedGBgTIjGB2x4TCKMAWYFwv1WGmE-nG43hmERZDedAAshBiJVXGRKJRXBoFPgwISmGBdggPrd7o8XkSwCsNuAyhAAO4ASQECBgdCpYBQ5EoYAQCiAA

Repro steps

If you want to reassign props (not modify), and use them in a callback function, React Compiler will skip compiling the component. For example:

export default function Component({ foo, bar }) {
	if (!foo && !bar) {
		foo = "foo";
	}

	return (
		<div>
			{Array.from({ length: 3 }, (_, i) => <div key={i}>{foo}</div>)}
		</div>
	);
}

I got this error:

Todo: Support destructuring of context variables

You can also replace the if statement if (!foo && !bar) { foo = "foo"; } with the content itself foo = "foo";, that will also trigger the issue, however it is meaningless.

The modified prop must in a callback, directly use it will work fine. For example, <div>{foo}</div> works fine, and <div>{(() => foo)()}</div> raise an issue.

I think the code above should be automatically converted into the code below which can work properly. Although I can also use the code below directly, it will only cause more trouble.

export default function Component({ foo: _foo, bar }) {
	let foo = _foo;
	if (!foo && !bar) {
		foo = "foo";
	}

	return (
		<div>
			{Array.from({ length: 3 }, (_, i) => <div key={i}>{foo}</div>)}
		</div>
	);
}

How often does this bug happen?

Every time

What version of React are you using?

19.2.1

What version of React Compiler are you using?

1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugType: Bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions