Skip to content

Commit 93b3735

Browse files
hannojgmeta-codesync[bot]
authored andcommitted
fix(android): fix bad_optional_access when zIndex changes to undefined with props 2.0 (#56288)
Summary: When Android props 2.0 diffs a `View` where `zIndex` changes from a value to `undefined`, `HostPlatformViewProps::getDiffProps` calls `zIndex.value()` and throws `bad_optional_access`: https://github.com/user-attachments/assets/89752243-ff48-42c7-a584-b47a091b4a4d This change emits `folly::dynamic(nullptr)` when `zIndex` is cleared instead. ## Changelog: [ANDROID] [FIXED] - Fixed a crash when clearing `zIndex` with props 2.0 enabled Pull Request resolved: #56288 Test Plan: Use the repro in `packages/rn-tester/js/examples/Playground/RNTesterPlayground.js`: ```js function Playground() { const [toggle, setToggle] = React.useState(false); return ( <View style={styles.container}> <Button title="Toggle" onPress={() => setToggle(t => !t)} /> <View style={{ backgroundColor: toggle ? 'blue' : 'red', height: 100, width: 100, position: 'absolute', zIndex: toggle ? undefined : 1, }} /> </View> ); } ``` 1. Enable Android props 2.0: ``` override fun enableAccumulatedUpdatesInRawPropsAndroid(): Boolean = true override fun enableExclusivePropsUpdateAndroid(): Boolean = true override fun enablePropsUpdateReconciliationAndroid(): Boolean = true ``` 2. run RNTester Playground on Android, and press `Toggle` to switch `zIndex` between `1` and `undefined`. This is with the issue fixed: https://github.com/user-attachments/assets/11253c23-b235-4e70-89a3-0d7bd07937f5 Reviewed By: javache Differential Revision: D98960669 Pulled By: NickGerleman fbshipit-source-id: d4a98434c599d2298c5cfc8c6895988ed0743af6
1 parent 5964a19 commit 93b3735

File tree

1 file changed

+2
-1
lines changed
  • packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view

1 file changed

+2
-1
lines changed

packages/react-native/ReactCommon/react/renderer/components/view/platform/android/react/renderer/components/view/HostPlatformViewProps.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ folly::dynamic HostPlatformViewProps::getDiffProps(
623623
}
624624

625625
if (zIndex != oldProps->zIndex) {
626-
result["zIndex"] = zIndex.value();
626+
result["zIndex"] =
627+
zIndex.has_value() ? zIndex.value() : folly::dynamic(nullptr);
627628
}
628629

629630
if (boxShadow != oldProps->boxShadow) {

0 commit comments

Comments
 (0)