react: [Compiler Bug]: `enableFunctionOutlining` breaks `react-native-reanimated` API callbacks
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-compiler (build issue installing or using the eslint plugin)
- react-compiler-healthcheck (build issue installing or using the healthcheck script)
Link to repro
Repro steps
Current version the Compiler started to transform react-native-reanimated API callbacks too much, breaking the code. I compared it with an older version babel-plugin-react-compiler@19.0.0-beta-9ee70a1-20241017 and the problem didn’t happen there. It seems to be due to the enableFunctionOutlining feature.
Given the code:
const TestComponent = ({ number }) => {
const keyToIndex = useDerivedValue(() =>
[1, 2, 3].map(() => null)
);
return null;
};
Compiler changes it to
const TestComponent = (t0) => {
useDerivedValue(_temp2);
return null;
};
function _temp() {
return null;
}
function _temp2() {
return [1, 2, 3].map(_temp);
}
Note that _temp and _temp2 are extracted outside. It’s ok to extract _temp2, the callback of useDerivedValue, it’s handled on our side as a part of the support for the Compiler. It’s not ok to extract _temp.
The problem lies in the fact that we can (and we do) deduce that _temp2 must be a worklet, but we cannot in general infer if _temp should be a worklet. This leads to _temp not being workletized and causing a runtime error. We also can’t workletize it regardless, in some flows it actually shouldn’t be a worklet.
To fix it we need to keep all functions defined in a worklet to stay in this worklet.
How often does this bug happen?
Every time
What version of React are you using?
19
What version of React Compiler are you using?
19.0.0-beta-bafa41b-20250307
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 19 (7 by maintainers)
Ok cool, we can add a property to outlined function declarations to make this clear to subsequent passes.
Yes, that’s what we do right now, I just don’t know the full extent of the changes the Compiler introduces so I’m trying to play it safe. I couldn’t break it with the current version of the online playground so maybe I’m just too careful. I think we can try the approach you are suggesting and circle back on it in the future if there’s any trouble.
I always thought it was the part of the awful documentation 🙈
I initially thought this issue could be resolved using the Reanimated’s babel plugin, but I it’s a bit counterintuitive for the compiler to extract functions for Reanimated only to move them back inside the scope.
A potential improvement would be to allow the
"use no memo"directive at the function level, not just the component level. This way, the Reanimated’s babel plugin could explicitly mark individual worklet functions with"use no memo", preventing unnecessary extraction.Alternatively,
"use server"and"use worklet"could implicitly act as"use no memo", since functions marked with these directives are already meant to be executed in specific, isolated environments. This would eliminate the need for an additional directive while keeping the behavior more predictable.