I recently used Proptypes and TS when bridging native code. The project is written in TypeScript on the React side, and I abstract away my native component on the React side in its own file. There was no need for worrying about PropTypes had this not been in its own file since I am already validating the data via TypeScript.
The PropTypes are used to handle external data coming in from Swift on an event callback. I tried using TypeScript here instead of PropTypes, but I was having issues with referencing the React components.
Ultimately, it was easier to implement PropTypes and doesn't seem to have drawbacks, since data validation at runtime worked perfectly fine.
Please refer to the code here for more detail:
//CoreView.js
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {requireNativeComponent, UIManager, findNodeHandle} from 'react-native';
const COMPONENT_NAME = 'CoreView';
const RNCoreView = requireNativeComponent(COMPONENT_NAME);
export default class CoreView extends Component {
static propTypes = {
label: PropTypes.array,
onUpdate: PropTypes.func,
};
_onUpdate = event => {
if (this.props.onUpdate) {
this.props.onUpdate(event.nativeEvent);
}
};
render() {
const {label, style} = this.props;
return (
<RNCoreView
style={style}
label={label}
onUpdate={this._onUpdate}
ref={ref => (this.ref = ref)}
/>
);
}
update = (...args) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this.ref),
UIManager[COMPONENT_NAME].Commands.obtainLabelData,
[...args],
);
};
}
And on the native side:
//CoreViewManager.m
#import <Foundation/Foundation.h>
#import "React/RCTViewManager.h"
@interface RCT_EXTERN_MODULE(CoreViewManager, RCTViewManager)
//Allow React to send data as props
RCT_EXPORT_VIEW_PROPERTY(onUpdate, RCTDirectEventBlock)
RCT_EXTERN_METHOD(
obtainLabelData:(nonnull NSNumber *)node
imageLocation:(nonnull NSString *)imageLocation
)
@end
as well as...
import Foundation
@available(iOS 11.0, *)
@objc(CoreViewManager)
class CoreViewManager: RCTViewManager {
override func view() -> UIView! {
return CoreView()
}
@objc func obtainLabelData(_ node: NSNumber, imageLocation: NSString!) {
DispatchQueue.main.async {
let component = self.bridge.uiManager.view(
forReactTag: node
) as! CoreView
component.update(value: imageLocation)
}
}
}