
[英]React native nativemodules not defined when linking a library with it
[英]React Native NativeModules pass multiple functions in one object
我正在使用React Native,试图创建一个可以接受NSDictionary函数的本机模块。 例如,假设我要调用一个具有多个潜在回调的函数。 假设我可以有0个回调,15个或介于两者之间的任何数字。 我使用字典,因此只包含要使用的回调函数:
MyModule.showInterstitial({
onLoad:()=>{console.log("Loaded")},
failedToLoad:()=>{console.log("Failed to load")},
onDismiss:()=>{console.log("Did dismiss")},
})
这更有意义的是使用API的人使代码看起来像这样,而不是像下面这样的东西,其中有15个不同的回调并且只使用了3个:
MyModule.showInterstitial(null,
null,
null,
null,
()=>{console.log("Loaded")},
null,
null,
()=>{console.log("Failed to load")},
null,
null,
null,
null,
null,
null,
()=>{console.log("Did dismiss")},
null
})
当我看我的Objective C代码时,我可以尝试创建一个这样的方法:
RCT_EXPORT_METHOD(showInterstitial: (NSDictionary *)callbacks) {
// implementation
}
但是,这种ObjC方法的问题在于NSDictionary创建了NSDictionaries的NSDictionary(换句话说,RCTConvert没有将JavaScript函数转换为RCTResponseSenderBlock的方法)。 我已经尝试过投放它,但它不起作用。 如果我只是尝试调用它,我的应用程序就会崩溃。
因此,我想知道是否有人曾经处理过此事或知道如何处理?
对于这样的用例,我认为您应该考虑在JavaScript中使用事件API,并使桥模块扩展RCTEventEmitter
。 桥接器基础结构更多地用于数据输入和输出,而不是功能。 您可能会发生load
和dismiss
事件,桥模块将触发这些事件,并且您的JS可以根据需要进行订阅。 showInterstitial
方法可以使用return回调来返回同步错误/成功。
更改桥模块以扩展RCTEventEmitter
,然后在需要触发加载和关闭事件时使用[self sendEventWithName:body:]
。
希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.