Skip to content
Snippets Groups Projects
Commit 81ddde00 authored by Sathya Gunsasekaran's avatar Sathya Gunsasekaran
Browse files

[compiler] Add tests for incorrect global mutation detection

If a function expression that mutates a global is passed as a prop,
we don't throw an error as we assume it's not called in render.

But if this function expression is captured in an object and passed down
as prop, we throw an error.

ghstack-source-id: 8d0655aac992b09d327a60123b7e96ea1d678d80
Pull Request resolved: https://github.com/facebook/react/pull/30456
parent e4b4aac2
No related merge requests found
## Input
```javascript
function Foo() {
const x = () => {
window.href = "foo";
};
const y = { x };
return <Bar y={y} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
};
```
## Error
```
1 | function Foo() {
2 | const x = () => {
> 3 | window.href = "foo";
| ^^^^^^ InvalidReact: Writing to a variable defined outside a component or hook is not allowed. Consider using an effect (3:3)
4 | };
5 | const y = { x };
6 | return <Bar y={y} />;
```
\ No newline at end of file
function Foo() {
const x = () => {
window.href = "foo";
};
const y = { x };
return <Bar y={y} />;
}
export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [],
};
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment