Last active
April 15, 2018 05:47
-
-
Save almost/e1f118c971abea162c3b to your computer and use it in GitHub Desktop.
Examples from Custom iOS Views in React Native on almostobsolete.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MyCustomView.h" | |
#import <UIKit/UIKit.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RCT_EXPORT_VIEW_PROPERTY(setRed, BOOL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createReactIOSNativeComponentClass = require('createReactIOSNativeComponentClass'); | |
var MyCustomView = createReactIOSNativeComponentClass({ | |
validAttributes: {isRed: true}, | |
uiViewClassName: 'MyCustomView', | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIView *)view | |
{ | |
MyCustomView * theView; | |
theView = [[MyCustomView alloc] init]; | |
return theView; | |
} |
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
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.