Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

How to pass callback function on a UINativeModule from React Native

$
0
0

I am accessing a lot of Swift functions, in my react native app, thanks to the native bridge module.

But I am stuck with an this issue:I have to call a function on an instance of Swift from React Native with three parameters, 2 String and 1 callback.

My Swift UIView:

// MyView.swiftclass MyView: UIView {  @objc func update(_ count: Int, forCb callback: @escaping RCTResponseSenderBlock) {    if ([some condition]) {      callback["error"]    }  }}
// MyViewManager.swift@objc(MyViewManager)class MyViewManager: RCTViewManager {  override func view() -> UIView! {    return MyView()  }  override static func requiresMainQueueSetup() -> Bool {    return true  }  @objc func updateFromManager(_ node: NSNumber, refCount count: NSNumber, refCb callback: @escaping RCTResponseSenderBlock) {    DispatchQueue.main.async {      let component = self.bridge.uiManager.view(        forReactTag: node      ) as! MyView      component.update(count, refCb: callback)    }  }}

My React Nativebridge Objective C:

// MyViewManager.m#import "React/RCTViewManager.h"@interface RCT_EXTERN_MODULE(MyViewManager, RCTViewManager)  RCT_EXTERN_METHOD(    updateFromManager:(nonnull NSNumber *)node refCount:(NSInteger)count forCb:(RCTResponseSenderBlock)callback  )@end

and then in my JS Code:

// App.js[...]myFunc = () => {  UIManager.dispatchViewManagerCommand(        findNodeHandle(this.ref),        UIManager.MyView.Commands.updateFromManager,        [1993, () => console.log('here is my callback)],      );}[...]

But i have this error:

ExceptionsManager.js:44 Invariant Violation: [215,1,[3614272248328,"<<Function >>"]] is not usable as a native method argument

Obviously, I can't return the callback in JS that way, do you have an idea ?I need an information from my MyView UI Component, in my Js, and I dont know how to pass it without callbcak.


Viewing all articles
Browse latest Browse all 16750

Trending Articles