How to control the existing React Native view instance from another native module

Takuya Matsuyama - Feb 19 '21 - - Dev Community

I've written how to inter-communicate between native modules in a React Native project:

Now, it's possible to get a View instance from another native module.
That allows you to control the existing view such as injecting JS into WebView without going through the React Native JS bridge, which is way performant.

Get React View Tag

Every view component has a react tag, which is a view instance identifier managed by RCTUIManager in iOS and UIManagerModule in Android.
You can get a view tag in the JS app like so:

import { WebView } from 'react-native-webview'

const YourComponent = (props) => {
  const webViewRef = useRef()
  useEffect(() => {
    const { current: webView } = webViewRef
    if (webView) {
      console.log(webView.webViewRef.current._nativeTag)
    }
  }, [])

  return (
    <WebView
      ref={webViewRef}
      ...
    />
  )
}
Enter fullscreen mode Exit fullscreen mode

Well, let's get the view instance from your native module.

iOS

RCTUIManager provides a method to get a view instance from a react tag:

- (UIView *)viewForReactTag:(NSNumber *)reactTag
Enter fullscreen mode Exit fullscreen mode

Prepare your bridge module to have access to the RCTBridge by following the steps described in the above post.
In your native bridge, you have to run on the main queue because views should be always controlled on the same thread.
For example, you can get an instance of WebView component by reactTag like so:

#import <React/RCTUIManager.h>

RCT_EXPORT_METHOD(runJS:(NSString* __nonnull)js
                  inView:(NSNumber* __nonnull)reactTag
                  withResolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
  RCTUnsafeExecuteOnMainQueueSync(^{
    RCTUIManager* uiManager = [self.bridge moduleForClass:[RCTUIManager class]];
    RNCWebView* webView = (RNCWebView*)[uiManager viewForReactTag:reactTag];
    if (webView) {
      [webView injectJavaScript:js];
    }
    resolve(@"OK");
  });
}
Enter fullscreen mode Exit fullscreen mode

It runs the given JS code in the webview with the specified tag.

Android

UIManagerModule provides a method to get a view instance from a react tag:

public View resolveView(int tag)
Enter fullscreen mode Exit fullscreen mode

As described in the above post, you can get an instance of UIManagerModule from reactContext.
Then, you can get a view by doing so:

  @ReactMethod
  public void injectJavaScript(int reactTag, String js) {
      UIManagerModule uiManagerModule = this.reactContext.getNativeModule(UIManagerModule.class);
      WebView webView = (WebView) uiManagerModule.resolveView(reactTag);
      webView.post(new Runnable() {
          @Override
          public void run() {
              webView.evaluateJavascript(js, null);
          }
      });
  }
Enter fullscreen mode Exit fullscreen mode

Now you can control the existing views from your native module.
Hope that helps!


Subscribe Newsletter

My YouTube channel

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .