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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Hanno J. Gödecke authored and meta-codesync[bot] committed 2026-03-31 18:01:10 -07:00
1 parent 5964a197a3
commit 93b37350af
1 file changed
+2 -1
@@ -623,7 +623,8 @@ folly::dynamic HostPlatformViewProps::getDiffProps(
}
if (zIndex != oldProps->zIndex) {
result["zIndex"] = zIndex.value();
result["zIndex"] =
zIndex.has_value() ? zIndex.value() : folly::dynamic(nullptr);
}
if (boxShadow != oldProps->boxShadow) {