Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,11 @@ export function isUseInsertionEffectHookType(id: Identifier): boolean {
id.type.shapeId === 'BuiltInUseInsertionEffectHook'
);
}
export function isUseEffectEventType(id: Identifier): boolean {
return (
id.type.kind === 'Function' && id.type.shapeId === 'BuiltInUseEffectEvent'
);
}

export function isUseContextHookType(id: Identifier): boolean {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IdentifierId,
isSetStateType,
isUseEffectHookType,
isUseEffectEventType,
isUseInsertionEffectHookType,
isUseLayoutEffectHookType,
isUseRefType,
Expand Down Expand Up @@ -98,6 +99,21 @@ export function validateNoSetStateInEffects(
instr.value.kind === 'MethodCall'
? instr.value.receiver
: instr.value.callee;

if (isUseEffectEventType(callee.identifier)) {
Copy link
Contributor

@jorge-cab jorge-cab Nov 25, 2025

Choose a reason for hiding this comment

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

This would only catch useEffectEvent function type calls at the top level in a useEffect right? Should we consider something like this?

const [state, setState] = useState(0)
const effectEvent = useEffectEvent(() => {
        setState(true)
    });

useEffect(() => {
    function foo() {
        effectEvent()
    }
    
    someLogic();
    foo();
}, [])

I'm just curious. Would this code even work? I'm not very familiar with uEE

Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's anything to specific about uEE in that example: it's just a question of whether we check recursive function expression (looks like maybe not, would be an easy extension). But that's an orthogonal change.

Copy link
Member

@josephsavona josephsavona Nov 25, 2025

Choose a reason for hiding this comment

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

I double-checked, we don't currently catch this case if you swap the effectEvent() call for a direct setState() call. Definitely worth adding — great catch, @jorge-cab! — but not strictly related to this PR

const arg = instr.value.args[0];
if (arg !== undefined && arg.kind === 'Identifier') {
const setState = setStateFunctions.get(arg.identifier.id);
if (setState !== undefined) {
/**
* This effect event function calls setState synchonously,
* treat it as a setState function for transitive tracking
*/
setStateFunctions.set(instr.lvalue.identifier.id, setState);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

can do else if to connect w the existing condition, since they can't both be true


if (
isUseEffectHookType(callee.identifier) ||
isUseLayoutEffectHookType(callee.identifier) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

## Input

```javascript
// @loggerTestOnly @validateNoSetStateInEffects
import {useEffect, useEffectEvent, useState} from 'react';

function Component() {
const [state, setState] = useState(0);
const effectEvent = useEffectEvent(() => {
setState(true);
});
useEffect(() => {
effectEvent();
}, []);
return state;
}

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @loggerTestOnly @validateNoSetStateInEffects
import { useEffect, useEffectEvent, useState } from "react";

function Component() {
const $ = _c(4);
const [state, setState] = useState(0);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
setState(true);
};
$[0] = t0;
} else {
t0 = $[0];
}
const effectEvent = useEffectEvent(t0);
let t1;
if ($[1] !== effectEvent) {
t1 = () => {
effectEvent();
};
$[1] = effectEvent;
$[2] = t1;
} else {
t1 = $[2];
}
let t2;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t2 = [];
$[3] = t2;
} else {
t2 = $[3];
}
useEffect(t1, t2);
return state;
}

```

## Logs

```
{"kind":"CompileError","detail":{"options":{"category":"EffectSetState","reason":"Calling setState synchronously within an effect can trigger cascading renders","description":"Effects are intended to synchronize state between React and external systems such as manually updating the DOM, state management libraries, or other platform APIs. In general, the body of an effect should do one or both of the following:\n* Update external systems with the latest state from React.\n* Subscribe for updates from some external system, calling setState in a callback function when external state changes.\n\nCalling setState synchronously within an effect body causes cascading renders that can hurt performance, and is not recommended. (https://react.dev/learn/you-might-not-need-an-effect)","suggestions":null,"details":[{"kind":"error","loc":{"start":{"line":10,"column":4,"index":267},"end":{"line":10,"column":15,"index":278},"filename":"invalid-setState-in-useEffect-via-useEffectEvent.ts","identifierName":"effectEvent"},"message":"Avoid calling setState() directly within an effect"}]}},"fnLoc":null}
{"kind":"CompileSuccess","fnLoc":{"start":{"line":4,"column":0,"index":108},"end":{"line":13,"column":1,"index":309},"filename":"invalid-setState-in-useEffect-via-useEffectEvent.ts"},"fnName":"Component","memoSlots":4,"memoBlocks":3,"memoValues":3,"prunedMemoBlocks":0,"prunedMemoValues":0}
```

### Eval output
(kind: exception) Fixture not implemented
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @loggerTestOnly @validateNoSetStateInEffects
import {useEffect, useEffectEvent, useState} from 'react';

function Component() {
const [state, setState] = useState(0);
const effectEvent = useEffectEvent(() => {
setState(true);
});
useEffect(() => {
effectEvent();
}, []);
return state;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

## Input

```javascript
// @validateNoSetStateInEffects @loggerTestOnly @compilationMode:"infer"
import {useEffect, useEffectEvent, useState} from 'react';

const shouldSetState = false;

function Component() {
const [state, setState] = useState(0);
const effectEvent = useEffectEvent(() => {
setState(10);
});
useEffect(() => {
setTimeout(effectEvent, 10);
});

const effectEventWithTimeout = useEffectEvent(() => {
setTimeout(() => {
setState(20);
}, 10);
});
useEffect(() => {
effectEventWithTimeout();
}, []);
return state;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime"; // @validateNoSetStateInEffects @loggerTestOnly @compilationMode:"infer"
import { useEffect, useEffectEvent, useState } from "react";

const shouldSetState = false;

function Component() {
const $ = _c(7);
const [state, setState] = useState(0);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
setState(10);
};
$[0] = t0;
} else {
t0 = $[0];
}
const effectEvent = useEffectEvent(t0);
let t1;
if ($[1] !== effectEvent) {
t1 = () => {
setTimeout(effectEvent, 10);
};
$[1] = effectEvent;
$[2] = t1;
} else {
t1 = $[2];
}
useEffect(t1);
let t2;
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
t2 = () => {
setTimeout(() => {
setState(20);
}, 10);
};
$[3] = t2;
} else {
t2 = $[3];
}
const effectEventWithTimeout = useEffectEvent(t2);
let t3;
if ($[4] !== effectEventWithTimeout) {
t3 = () => {
effectEventWithTimeout();
};
$[4] = effectEventWithTimeout;
$[5] = t3;
} else {
t3 = $[5];
}
let t4;
if ($[6] === Symbol.for("react.memo_cache_sentinel")) {
t4 = [];
$[6] = t4;
} else {
t4 = $[6];
}
useEffect(t3, t4);
return state;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};

```

## Logs

```
{"kind":"CompileSuccess","fnLoc":{"start":{"line":6,"column":0,"index":164},"end":{"line":24,"column":1,"index":551},"filename":"valid-setState-in-useEffect-via-useEffectEvent-listener.ts"},"fnName":"Component","memoSlots":7,"memoBlocks":5,"memoValues":5,"prunedMemoBlocks":0,"prunedMemoValues":0}
```

### Eval output
(kind: exception) (0 , _react.useEffectEvent) is not a function
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @validateNoSetStateInEffects @loggerTestOnly @compilationMode:"infer"
import {useEffect, useEffectEvent, useState} from 'react';

const shouldSetState = false;

function Component() {
const [state, setState] = useState(0);
const effectEvent = useEffectEvent(() => {
setState(10);
});
useEffect(() => {
setTimeout(effectEvent, 10);
});

const effectEventWithTimeout = useEffectEvent(() => {
setTimeout(() => {
setState(20);
}, 10);
});
useEffect(() => {
effectEventWithTimeout();
}, []);
return state;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
Loading