maxs15 / react-native-modalbox

A <Modal/> component for react-native
MIT License
2.9k stars 506 forks source link

Fix: Crash index out of bounds when open multiple modal the second time #340

Open rifansyah opened 3 years ago

rifansyah commented 3 years ago

What is going on?

We got crash when having a multiple modal, then open the top modal a second time.

The crash error:

java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
    at ...

Level: {crash}

How to reproduce?

Condition

Step

open the top modal, close it, and open it again

Caused by

when the modal is not visible, the library will only return View without zIndex props, and when it's visible, it'll return a view with a zIndex style props.

I'm not sure about the cause, but because the js side and native side is asynchronously connected, then maybe when the modal is about to be opened, the native side is trying to reOrder the view with zIndex (at com.facebook.react.uimanager.ViewGroupDrawingOrderHelper.getChildDrawingOrder) while the View with zIndex props is not ready yet. That's why we got java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

Resolved by

We return a View with the same zIndex (use styles.transparent) when the modal is not visible, in this way, the view group will have the same number of view with a zIndex props to prevent a not ready view whether it's visible or not.

kbanashek commented 2 years ago

This should be the accepted answer, thank you for providing this! @rifansyah