I have an existing app that I am working on integrating React-Native for a portion of it. I am having trouble understanding how to 'exit' react-native and get back to a native view.
Here's some code:
// Main objective-c code that bootstraps the react-native view. This view is loaded as a modal view.
MainViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"IndexApp" initialProperties:props launchOptions:nil];
rootView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-49);
[self.view addSubview:rootView];
}
My initial react views are such:
render() {
return (
<Navigator
style={styles.container}
...
I have a right-nav button on the Navigator that I would like to "dismiss" the react view and the underlying MainViewController native view.
I have tried a callback to MainViewController from the react view like so, but without avail:
RCT_EXPORT_METHOD(dismiss:(NSString *)name location:(NSString *)location)
{
NSLog(@"dismiss...");
// these don't do anything
//[self dismissViewControllerAnimated:YES completion:nil];
//[self.navigationController popViewControllerAnimated:YES];
// anything with _rootView doesn't do anything, i.e. _rootView removeFromSuperview];
}
Any help with an approach to 'exit' the react-native view and get back into native views would be appreciated.
The only way I've found this works is this
The gist of it is:
You need to run the pop or dismiss on the main thread:
RCT_EXPORT_METHOD(dismiss:(NSString *)name location:(NSString *)location)
{
NSLog(@"dismiss...");
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
// use pop instead if this view controller was pushed onto a navigation controller
//[self.navigationController popViewControllerAnimated:YES];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With