colorfy-software / react-native-modalfy

šŸ„ž Modal citizen of React Native.
https://colorfy-software.gitbook.io/react-native-modalfy
MIT License
1.16k stars 48 forks source link

Crash report when calling closeAllModals() and openModal(), undefined is not an object #78

Closed okcompewter closed 2 years ago

okcompewter commented 2 years ago

Breaking out of Issue 73, I've updated to 3.2.0, and it seems to have fixed a lot of errors, but I'm still seeing this one crash log in production occasionally:

Are you sure the recent optional chaining fix doesn't need done elsewhere as well?

When looking at your fix, before the slice check: currentModalHash: [...currentState.stack.openedItems].slice(-1)[0]?.hash,

may it be possible that any of these are also undefined: currentState or currentState.stack or currentState.stack.openedItems ?

The crash:

Fatal Exception: com.facebook.react.common.c: TypeError: undefined is not an object (evaluating 'f.default.getState().currentModal')

This error is located at:
    in l
    in p
    in RCTView
    in Unknown
    in R
    in Unknown
    in RCTView
    in Unknown
    in R
    in Unknown
    in p
    in y
    in P
    in RCTView
    in Unknown
    in l
    in RCTView
    in Unknown
    in R
    in Unknown
    in PanGestureHandler
    in S
    in w
    in RCTView
    in Unknown
    in v
    in l
    in C
    in x
    in p
    in RCTView
    in Unknown
    in M
    in b
    in C
    in l
    in v
    in Unknown
    in Unknown
    in Unknown
    in Unknown
    in B
    in Unknown
    in RCTView
    in Unknown
    in RCTView
    in Unknown
    in b, stack:
C:\Users\...\node_modules\react-native-modalfy\src\lib\ModalProvider.tsx:39:openModal
C:\Users\...\app\screens\raid-screen\raid-screen.tsx:29:useEffect$argument_0
C:\Users\...\node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-prod.js:5467:commitHookEffectListMount
C:\Users\...\node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-prod.js:7003:flushPassiveEffects
C:\Users\...\node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-prod.js:6849:scheduleCallback$argument_1
C:\Users\...\node_modules\scheduler\cjs\scheduler.production.min.js:16:V
C:\Users\...\node_modules\scheduler\cjs\scheduler.production.min.js:10:w
C:\Users\...\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:214:_allocateCallback$argument_0
C:\Users\...\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:112:_callTimer
C:\Users\...\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:357:callTimers
C:\Users\...\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:419:__callFunction
C:\Users\...\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:116:__guard$argument_0
C:\Users\...\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:370:__guard
C:\Users\...\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:115:callFunctionReturnFlushedQueue
value@-1

       at com.facebook.react.modules.core.ExceptionsManagerModule.reportException(ExceptionsManagerModule.java:83)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
       at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188)
       at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
       at java.lang.Thread.run(Thread.java:920)
okcompewter commented 2 years ago

Any update on this issue? Still seeing a lot of non-fatal errors and crashes with it. Thank you!

CharlesMangwa commented 2 years ago

Hi @okcompewter! Thanks for creating a separate issue! šŸ‘ The stack trace was quite revealing. By the looks of it, the issue comes from ModalProvider.tsx:39:

https://github.com/colorfy-software/react-native-modalfy/blob/8c5aa9258114d30335e8e12595434622941edb18/src/lib/ModalProvider.tsx#L38-L39

Which would mean that openModal() is being invoked before the stack could be initialized:

https://github.com/colorfy-software/react-native-modalfy/blob/8c5aa9258114d30335e8e12595434622941edb18/src/lib/ModalProvider.tsx#L103-L109

I'll add a fix in v3.3.0 today that would give a default value to the internal state which was undefined up until now:

https://github.com/colorfy-software/react-native-modalfy/blob/8c5aa9258114d30335e8e12595434622941edb18/src/lib/ModalState.ts#L19

I think that should fix the crashes you're getting but at the same time: you might want to investigate where openModal() is being called so early.

okcompewter commented 2 years ago

I figured that was likely the issue (stack not yet initialized when it is called). It is a bit of a mystery to me how this could even be happening though...

I will update with this fix today! Thanks so much šŸ‘