Skip to content

[compiler] Allow ref access in ResizeObserver / MutationObserver callbacks - #37895

Merged
javache merged 1 commit into
react:mainfrom
zhoubiao188:scan-2026-10-19
Oct 23, 2026
Merged

javache merged 1 commit into
react:mainfrom
zhoubiao188:scan-2026-10-19

Conversation

@zhoubiao188

@zhoubiao188 zhoubiao188 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

new ResizeObserver(() => ref.current) and new MutationObserver(() => ref.current) currently report a false positive:

Error: Cannot access refs during render
  > 6 |   return new ResizeObserver(() => ref.current);
      |                             ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render

Both constructors only retain the callback and invoke it asynchronously after the observation fires — never during render — so reading a ref inside the callback is safe. The same reasoning was applied to IntersectionObserver in #37764; the remaining two DOM observer constructors were still unmodeled and fell back to the default effects for unknown constructors.

This PR models both the same way in the TypeScript and Rust compilers:

  • ResizeObserver(callback): single Freezed callback. (Unlike IntersectionObserver, whose options object is a second constructor argument, the {box} option of ResizeObserver belongs to observe(target, options).)
  • MutationObserver(callback): single Freezed callback.

Validation

  • allow-ref-access-in-resize-observer.ts: plain callback compiles, and observe(target, {box}) options do not trip the validation.
  • allow-ref-access-in-mutation-observer.ts: callback compiles.
  • error.invalid-mutate-hook-value-in-resize-observer.js: mutating a useMemo value inside the callback is still rejected (This value cannot be modified), i.e. the Freeze effect is preserved and only the ref false positive is removed.
  • Unknown constructors are unaffected: new Observer(() => ref.current) still errors.
  • Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical to the pre-existing baseline in this environment (fbt/*, debugger*, conflict-codegen-instrument-forget), so no new snapshot deltas.
  • tsc --noEmit and prettier check-changed pass.

Notes

The observers' instance methods (observe/unobserve/disconnect) are intentionally left unmodeled, matching IntersectionObserver.

@meta-cla meta-cla Bot added the CLA Signed label Oct 19, 2026
…backs

Both constructors only retain the callback and invoke it after render, so
reading a ref inside the callback is safe. Without a modeled signature they
fall back to the default effects for unknown constructors, which report

    Cannot access refs during render

for the common `new ResizeObserver(() => ref.current)` pattern. That is the
same false positive that react#37764 fixed for `IntersectionObserver`.

Model both observers in the TypeScript and Rust compilers. Each takes a
single constructor argument — unlike `IntersectionObserver`, whose options
object is a second constructor parameter, `ResizeObserver` and
`MutationObserver` receive their options in `observe(target, options)` — so
both are modeled with a single `Freeze`d callback.

The fixtures cover the accepted patterns (including `observe` options) plus
one negative case asserting that mutating a hook value inside the callback
is still rejected.
@javache
javache merged commit 23cd68d into react:main Oct 23, 2026
19 checks passed
github-actions Bot pushed a commit that referenced this pull request Oct 23, 2026
…backs (#37895)

## Summary

`new ResizeObserver(() => ref.current)` and `new MutationObserver(() =>
ref.current)` currently report a false positive:

```
Error: Cannot access refs during render
  > 6 |   return new ResizeObserver(() => ref.current);
      |                             ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render
```

Both constructors only *retain* the callback and invoke it
asynchronously after the observation fires — never during render — so
reading a ref inside the callback is safe. The same reasoning was
applied to `IntersectionObserver` in #37764; the remaining two DOM
observer constructors were still unmodeled and fell back to the default
effects for unknown constructors.

This PR models both the same way in the TypeScript and Rust compilers:

- `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike
`IntersectionObserver`, whose options object is a second constructor
argument, the `{box}` option of `ResizeObserver` belongs to
`observe(target, options)`.)
- `MutationObserver(callback)`: single `Freeze`d callback.

## Validation

- `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and
`observe(target, {box})` options do not trip the validation.
- `allow-ref-access-in-mutation-observer.ts`: callback compiles.
- `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a
`useMemo` value inside the callback is **still** rejected (`This value
cannot be modified`), i.e. the `Freeze` effect is preserved and only the
ref false positive is removed.
- Unknown constructors are unaffected: `new Observer(() => ref.current)`
still errors.
- Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical
to the pre-existing baseline in this environment (`fbt/*`, `debugger*`,
`conflict-codegen-instrument-forget`), so no new snapshot deltas.
- `tsc --noEmit` and `prettier check-changed` pass.

## Notes

The observers' instance methods (`observe`/`unobserve`/`disconnect`) are
intentionally left unmodeled, matching `IntersectionObserver`.

Co-authored-by: zhoubiao188 <[email protected]>

DiffTrain build for [23cd68d](23cd68d)
github-actions Bot pushed a commit that referenced this pull request Oct 23, 2026
…backs (#37895)

## Summary

`new ResizeObserver(() => ref.current)` and `new MutationObserver(() =>
ref.current)` currently report a false positive:

```
Error: Cannot access refs during render
  > 6 |   return new ResizeObserver(() => ref.current);
      |                             ^^^^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render
```

Both constructors only *retain* the callback and invoke it
asynchronously after the observation fires — never during render — so
reading a ref inside the callback is safe. The same reasoning was
applied to `IntersectionObserver` in #37764; the remaining two DOM
observer constructors were still unmodeled and fell back to the default
effects for unknown constructors.

This PR models both the same way in the TypeScript and Rust compilers:

- `ResizeObserver(callback)`: single `Freeze`d callback. (Unlike
`IntersectionObserver`, whose options object is a second constructor
argument, the `{box}` option of `ResizeObserver` belongs to
`observe(target, options)`.)
- `MutationObserver(callback)`: single `Freeze`d callback.

## Validation

- `allow-ref-access-in-resize-observer.ts`: plain callback compiles, and
`observe(target, {box})` options do not trip the validation.
- `allow-ref-access-in-mutation-observer.ts`: callback compiles.
- `error.invalid-mutate-hook-value-in-resize-observer.js`: mutating a
`useMemo` value inside the callback is **still** rejected (`This value
cannot be modified`), i.e. the `Freeze` effect is preserved and only the
ref false positive is removed.
- Unknown constructors are unaffected: `new Observer(() => ref.current)`
still errors.
- Full fixture suite: 1835 fixtures, 25 changed / 3 failed — identical
to the pre-existing baseline in this environment (`fbt/*`, `debugger*`,
`conflict-codegen-instrument-forget`), so no new snapshot deltas.
- `tsc --noEmit` and `prettier check-changed` pass.

## Notes

The observers' instance methods (`observe`/`unobserve`/`disconnect`) are
intentionally left unmodeled, matching `IntersectionObserver`.

Co-authored-by: zhoubiao188 <[email protected]>

DiffTrain build for [23cd68d](23cd68d)
javache pushed a commit that referenced this pull request Oct 26, 2026
…r callbacks (#37996)

## Summary

`new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are
compiled with a false positive when the callback touches a ref:

```
Error: Cannot access refs during render
  4 |   return new PerformanceObserver(() => {
    |                                  ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render
```

Both constructors only *retain* the callback and invoke it
asynchronously once the observation fires — never during render — so
reading (or writing) a ref from the callback is safe.
`IntersectionObserver` (#37764) and `ResizeObserver` /
`MutationObserver` (#37895) are already modelled exactly this way; these
two were just missed and fall back to the unknown-constructor default,
whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`.

This is the same class of fix as #37895, which is now on `main`.

## Fix

Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals`
(Rust) using the same signature shape as the existing observers:

| Global | Constructor signature |
| --- | --- |
| `PerformanceObserver` | `(callback)` → `[Freeze]` |
| `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` |

`ReportingObserver`'s second argument is a genuine constructor argument
(unlike the observers whose options belong to `observe()`), so it is
read rather than retained.

Both backends are updated so TS and Rust stay in parity.

## Tests

Four new fixtures:

- `allow-ref-access-in-performance-observer.ts`
- `allow-ref-access-in-reporting-observer.ts`

Both fail before the change with `Cannot access refs during render` and
compile to memoized constructors after it.

- `error.invalid-mutate-hook-value-in-performance-observer.js`
- `error.invalid-mutate-hook-value-in-reporting-observer.js`

Negative controls proving `Freeze` is not weakened: mutating a `useMemo`
value inside the callback still reports `` `entries` cannot be modified
``.

Validation run locally:

- Full fixture suite (1840 fixtures): `25 changed / 3 failed` —
identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new
diffs.
- `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean.
- Rust side mirrors the TS change 1:1 but was not compiled locally (no
toolchain in this environment); relying on the
`babel-plugin-react-compiler-rust` CI job.

Co-authored-by: zhoubiao188 <[email protected]>
github-actions Bot pushed a commit that referenced this pull request Oct 26, 2026
…r callbacks (#37996)

## Summary

`new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are
compiled with a false positive when the callback touches a ref:

```
Error: Cannot access refs during render
  4 |   return new PerformanceObserver(() => {
    |                                  ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render
```

Both constructors only *retain* the callback and invoke it
asynchronously once the observation fires — never during render — so
reading (or writing) a ref from the callback is safe.
`IntersectionObserver` (#37764) and `ResizeObserver` /
`MutationObserver` (#37895) are already modelled exactly this way; these
two were just missed and fall back to the unknown-constructor default,
whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`.

This is the same class of fix as #37895, which is now on `main`.

## Fix

Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals`
(Rust) using the same signature shape as the existing observers:

| Global | Constructor signature |
| --- | --- |
| `PerformanceObserver` | `(callback)` → `[Freeze]` |
| `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` |

`ReportingObserver`'s second argument is a genuine constructor argument
(unlike the observers whose options belong to `observe()`), so it is
read rather than retained.

Both backends are updated so TS and Rust stay in parity.

## Tests

Four new fixtures:

- `allow-ref-access-in-performance-observer.ts`
- `allow-ref-access-in-reporting-observer.ts`

Both fail before the change with `Cannot access refs during render` and
compile to memoized constructors after it.

- `error.invalid-mutate-hook-value-in-performance-observer.js`
- `error.invalid-mutate-hook-value-in-reporting-observer.js`

Negative controls proving `Freeze` is not weakened: mutating a `useMemo`
value inside the callback still reports `` `entries` cannot be modified
``.

Validation run locally:

- Full fixture suite (1840 fixtures): `25 changed / 3 failed` —
identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new
diffs.
- `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean.
- Rust side mirrors the TS change 1:1 but was not compiled locally (no
toolchain in this environment); relying on the
`babel-plugin-react-compiler-rust` CI job.

Co-authored-by: zhoubiao188 <[email protected]>

DiffTrain build for [c050dbd](c050dbd)
github-actions Bot pushed a commit that referenced this pull request Oct 26, 2026
…r callbacks (#37996)

## Summary

`new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are
compiled with a false positive when the callback touches a ref:

```
Error: Cannot access refs during render
  4 |   return new PerformanceObserver(() => {
    |                                  ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render
```

Both constructors only *retain* the callback and invoke it
asynchronously once the observation fires — never during render — so
reading (or writing) a ref from the callback is safe.
`IntersectionObserver` (#37764) and `ResizeObserver` /
`MutationObserver` (#37895) are already modelled exactly this way; these
two were just missed and fall back to the unknown-constructor default,
whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`.

This is the same class of fix as #37895, which is now on `main`.

## Fix

Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals`
(Rust) using the same signature shape as the existing observers:

| Global | Constructor signature |
| --- | --- |
| `PerformanceObserver` | `(callback)` → `[Freeze]` |
| `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` |

`ReportingObserver`'s second argument is a genuine constructor argument
(unlike the observers whose options belong to `observe()`), so it is
read rather than retained.

Both backends are updated so TS and Rust stay in parity.

## Tests

Four new fixtures:

- `allow-ref-access-in-performance-observer.ts`
- `allow-ref-access-in-reporting-observer.ts`

Both fail before the change with `Cannot access refs during render` and
compile to memoized constructors after it.

- `error.invalid-mutate-hook-value-in-performance-observer.js`
- `error.invalid-mutate-hook-value-in-reporting-observer.js`

Negative controls proving `Freeze` is not weakened: mutating a `useMemo`
value inside the callback still reports `` `entries` cannot be modified
``.

Validation run locally:

- Full fixture suite (1840 fixtures): `25 changed / 3 failed` —
identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new
diffs.
- `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean.
- Rust side mirrors the TS change 1:1 but was not compiled locally (no
toolchain in this environment); relying on the
`babel-plugin-react-compiler-rust` CI job.

Co-authored-by: zhoubiao188 <[email protected]>

DiffTrain build for [c050dbd](c050dbd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants