Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access native UI components' instance methods in React Native

Tags:

react-native

We can control custom native UI components' properties by exporting native properties using RCT_EXPORT_VIEW_PROPERTY.

But how to export instance methods of native UI components?

like image 381
Derek Hsu Avatar asked Aug 06 '15 13:08

Derek Hsu


People also ask

Does React Native use native UI components?

React Native comes with a set of essential, ready-to-use Native Components you can use to start building your app today. These are React Native's Core Components. React Native also lets you build your own Native Components for Android and iOS to suit your app's unique needs.

Which is UI component for React Native?

NativeBase is an accessible, utility-first component library that helps you build consistent UI across Android, iOS and Web. Interact with me!

What is RCTViewManager?

The RCTViewManager s are also typically the delegates for the views, sending events back to JavaScript via the bridge. To expose a view you can: Subclass RCTViewManager to create a manager for your component.

Can I use UI image in React Native?

Similarly, for the Image component, we use the UIImage component from iOS, so that all the components we use in React Native have a native declaration. React Native already wraps all the major and widely used UI components, such as ScrollView, Button, and Switch.

What is the use of requirenativecomponent in React Native?

The requireNativeComponent function takes the name of the native view. Note that if your component needs to do anything more sophisticated (e.g. custom event handling), you should wrap the native component in another React component. This is illustrated in the MyCustomView example below.

What are the best libraries to use with React Native?

React Native NativeBase Component has been the most used user interface component library, which provides multiple components for React Native. We can also use third-party libraries, which can help the project. 2. React Native Elements

What is UIView in React Native?

Under the hood, React Native uses native components like UIView and UIImageView in iOS and exports them to the JavaScript layer. We use these components in our daily development process. For example, when we use the View component in JSX, we are consuming UIView from iOS in our JavaScript code.


1 Answers

Thanks to @alinz's suggestion.

This can be done within the custom native component's view manager.

  1. Obj-c side: In the native view manager expose a native method like this:

The key is to pass in the reactTag which is the reference to the native component.

MyCoolViewManager.m:

RCT_EXPORT_METHOD(myCoolMethod:(NSNumber *)reactTag callback:(RCTResponseSenderBlock)callback) {
  [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry) {
    MyCoolView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[MyCoolView class]]) {
      RCTLogMustFix(@"Invalid view returned from registry, expecting MyCoolView, got: %@", view);
    }
    // Call your native component's method here
    [view myCoolMethod];
  }];
}
  1. JS side: Add API in the react component class:

MyCoolView.js:

var React = require('react-native');
var NativeModules = require('NativeModules');
var MyCoolViewManager = NativeModules.MyCoolViewManager;
var findNodeHandle = require('findNodeHandle');

class MyCoolView extends React.Component{
    // ...
    myCoolMethod() {
        return new Promise((resolve, reject) => {
            MyCoolViewManager.myCoolMethod(
                findNodeHandle(this),
                (error, result) => {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(result);
                    }
                }
            );
        });
    }
}
like image 151
Derek Hsu Avatar answered Oct 05 '22 18:10

Derek Hsu