Illegal callback invocation from native module error
See original GitHub issueDescription
After upgraded to 0.41, many modules don’t work and get this error:
java.lang.RuntimeException: Illegal callback invocation from native module. This callback type only permits a single invocation from native code.
at com.facebook.react.bridge.CallbackImpl.invoke(CallbackImpl.java:32)
I think it’s caused by this change in react-native which limits the callback to be invoked for only once:
Fail-Fast on Redundant Java Callback Invocations
@Override
public void invoke(Object... args) {
+ if (mInvoked) {
+ throw new RuntimeException("Illegal callback invocation from native "+
+ "module. This callback type only permits a single invocation from "+
+ "native code.");
+ }
mCatalystInstance.invokeCallback(mExecutorToken, mCallbackId, Arguments.fromJavaArgs(args));
+ mInvoked = true;
}
However, many modules would expect their callbacks to be invoked more than once. E.g. react-native-dialogs which shows native dialogs for Android. And on the dialog, we will probably invoke the callback for multiple times, e.g. select an option, press ok will both invoke the callback with different args.
Have we considered that sometimes we do need multiple callback invocations?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:9
Top Results From Across the Web
App crash with error: Illegal callback invocation from native ...
App crash with error : Illegal callback invocation from native module. This callback type only permits a single invocation from native code. #713....
Read more >React Native callback type only permits a single invocation ...
I am getting "Illegal callback invocation from native module. This callback type only permits a single invocation from native code." 3 · Getting ......
Read more >Calling a callback multiple times in a React Native module
You wanted a callback to be invoked multiple times, but got this error instead "Illegal callback invocation from native module.
Read more >Android Native Modules
Another important detail to note is that a native module method can only invoke one callback, one time. This means that you can...
Read more >Getting "Illegal callback invocation from native module. This ...
[Solved]-Getting "Illegal callback invocation from native module. This callback type only permits a single invocation from native code"-React Native.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Instead of using callbacks, use events with RCTEventEmitter
Example below using swift. I left out the .m since it isn’t relevant to event emitting with swift. The obj-c version is about the same and will be easy for you to google/figure out
edit: I put together a small blog post to explain this: https://medium.com/nycdev/calling-a-callback-multiple-times-in-a-react-native-module-5c3c61f2fca4
.swift
.js
Ok, here is the answer for now: Whenever you invoke a method from the javascript side on the module that may in turn invoke the callback you need to reset the callback:
resetCallback(){ MyModule.setTheCallback((value) => { this.setState({ value: value }); }); } invokeMethodThatMayInvokeCallback(){ resetCallback(); MyModule.bar(); // the method within the module that will invoke the callback. }