Skip to content

Fix: preserve optional chaining when inner fn in hook callback is con…#36119

Open
arsyadal wants to merge 1 commit intofacebook:mainfrom
arsyadal:fix/compiler-preserve-optional-chain-inner-fn-effect
Open

Fix: preserve optional chaining when inner fn in hook callback is con…#36119
arsyadal wants to merge 1 commit intofacebook:mainfrom
arsyadal:fix/compiler-preserve-optional-chain-inner-fn-effect

Conversation

@arsyadal
Copy link

@arsyadal arsyadal commented Mar 20, 2026

Summary:
Fixes #36057

The React Compiler was incorrectly removing optional chaining (?.) by
assuming a variable is always non-null, based on a non-optional property
access inside a conditionally-called inner function within a hook callback.

Root Cause

In CollectHoistablePropertyLoads.ts, when analyzing a hook callback
(e.g. useEffect), the compiler recursively collects non-null assumptions
from inner functions (e.g. log) that are in assumedInvokedFns. This
caused non-null assumptions from log to propagate to the hook callback's
entry block — even though log is only called conditionally
(if (currentDevice) log()).

These assumptions then propagated to the component's render scope, causing
optional chains like currentDevice?.type to be incorrectly rewritten as
currentDevice.type, resulting in a TypeError at runtime when
currentDevice is undefined.

Fix

In collectNonNullsInBlocks, skip recursive propagation from inner
FunctionExpression nodes when already inside a nested function context
(nestedFnImmutableContext !== null). This prevents non-null assumptions
from depth-2+ inner functions from leaking into the outer render scope.

Functions defined directly in the component scope (depth 0) are unaffected.

@meta-cla
Copy link

meta-cla bot commented Mar 20, 2026

Hi @arsyadal!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla
Copy link

meta-cla bot commented Mar 20, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Mar 20, 2026
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.

[Compiler Bug]: React Compiler removes optional chaining due to incorrect assumption from useEffect access

1 participant