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?
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.
NativeBase is an accessible, utility-first component library that helps you build consistent UI across Android, iOS and Web. Interact with me!
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.
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.
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.
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
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.
Thanks to @alinz's suggestion.
This can be done within the custom native component's view manager.
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];
}];
}
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);
}
}
);
});
}
}
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