2

I'm new to ReactNative and I'm trying Integrate React Native to my current project.

In my ViewController contain RCTRootView and the code implemented like:

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()        
    .....
        let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: nil)

        let reactRootView = RCTRootView(bridge: bridge, moduleName: "Index", initialProperties: props)

        self.view.addSubview(reactRootView);
        reactRootView.frame = self.view.bounds
        reactRootView.delegate = self;
    ....     
}

func onReactEvent(event :NSDictionary , callback: RCTResponseSenderBlock) {}

ReactNativeManager.swift

@objc func sendCustomEventToHost(event: NSDictionary, callback: RCTResponseSenderBlock) -> Void {}

So how can I call onReactEvent() function in my ViewController from function sendCustomEventToHost() in ReactNativeManager. Thanks

2 Answers 2

1

I know how to passing my ViewController to ReactNativeManager using launchOptions

ViewController.swift

let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: ["controller":self"])

Access from ReactNativeManager.swift

let controller = self.bridge.launchOptions["controller"]

Hope this help!!

Sign up to request clarification or add additional context in comments.

Comments

0

In ReactNativeManager you have to create an instance of ViewController. For example:

let viewC = ViewController()

and then you just call:

viewC.onReactEvent(....)

3 Comments

Because I want to use some variables in the ViewController which is initialised before. The code "let viewC = ViewController()" will init the new one have different address. I don't know how to call the method of the parent ViewController which contain RCTRootView
ReactNativeManager is pushed from ViewController?
ReactNativeManager class inherit from RCTBridgeModule, Initiated and its methods are called by React Native view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.