Skip to content

Instantly share code, notes, and snippets.

@almost
Last active April 15, 2018 05:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save almost/e1f118c971abea162c3b to your computer and use it in GitHub Desktop.
Save almost/e1f118c971abea162c3b to your computer and use it in GitHub Desktop.
Examples from Custom iOS Views in React Native on almostobsolete.net
#import "MyCustomView.h"
@implementation MyCustomView
{
UIColor *squareColor;
}
- (void)setIsRed:(BOOL)isRed
{
squareColor= (isRed) ? [UIColor redColor] : [UIColor greenColor];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[squareColor setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
var React = require('react-native');
var { AppRegistry, Image, Text } = React;
var App = React.createClass({
render: function() {
return (
<View>
<Image src={require('image!hello')}/>
<Text>Hello World!</Text>
</View>
);
}
});
AppRegistry.registerComponent('helloworld', () => App);
#import "MyCustomView.h"
#import <UIKit/UIKit.h>
/**
* Example: Native Components in React Native
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View
} = React;
var createReactIOSNativeComponentClass = require('createReactIOSNativeComponentClass');
var MyCustomView = createReactIOSNativeComponentClass({
validAttributes: {isRed: true},
uiViewClassName: 'MyCustomView',
})
var demo = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text>
Red one
</Text>
<MyCustomView style={{width: 10, height: 10}} isRed={true}/>
<Text>
Not red one
</Text>
<MyCustomView style={{width: 10, height: 10}} isRed={false}/>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
customView: {
width: 10,
height: 10
}
});
AppRegistry.registerComponent('demo', () => demo);
#import "MyCustomView.h"
@implementation MyCustomView
{
UIColor *squareColor;
}
- (void)setIsRed:(BOOL)isRed
{
squareColor= (isRed) ? [UIColor redColor] : [UIColor greenColor];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[squareColor setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
@end
RCT_EXPORT_VIEW_PROPERTY(setRed, BOOL)
render: function() {
return (
<View style={styles.container}>
<Text>
Red one
</Text>
<MyCustomView style={{width: 10, height: 10}} isRed={true}/>
<Text>
Not red one
</Text>
<MyCustomView style={{width: 10, height: 10}} isRed={false}/>
</View>
);
}
var createReactIOSNativeComponentClass = require('createReactIOSNativeComponentClass');
var MyCustomView = createReactIOSNativeComponentClass({
validAttributes: {isRed: true},
uiViewClassName: 'MyCustomView',
})
- (UIView *)view
{
MyCustomView * theView;
theView = [[MyCustomView alloc] init];
return theView;
}
@tehong
Copy link

tehong commented Nov 9, 2015

I don't understand why the "RCT_EXPORT_VIEW_PROPERTY(setRed, BOOL)" uses "setRed" where there was only "setIsRed" method in the MyCustomView.m code.

@ramakrishna013
Copy link

RCT_EXPORT_VIEW_PROPERTY is like a callback??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment