Eirik Langholm Vullum PRO
JavaScript fanatic that loves node.js and React.
What vs. how
element = createElement(...)
element.textContent = 'Submit form!';
element.style.backgroundColor = 'blue';
element.appendChild(...)
element.parentNode.parentNode...
<SubmitButton
color='blue'
text='Submit form!'
/>
Imperative (how)
Declarative (what)
UI = f(props, state)
const ChatApp = React.createClass({
...
render() {
return (
<div>
<FriendList friends={this.state.friends} />
<MessageList messages={this.state.messages} />
<MessageInput handleInput={this.handleInput} />
</div>
);
}
});
(Stockholm syndrome..?)
React Native enables you to build world-class application experiences on native platforms using a consistent developer experience based on JavaScript and React. The focus of React Native is on developer efficiency across all the platforms you care about — learn once, write anywhere. Facebook uses React Native in multiple production apps and will continue investing in React Native.
React.js | React Native | |
---|---|---|
Block | <div> | <View> |
Inline | <span> | <Text> |
Image | <img> | <Image> |
// React.js
const Component = React.createClass({
render() {
return (
<div>
<span>Hello World</span>
<img src='...' />
</div>
);
}
});
// React Native
const Component = React.createClass({
render() {
return (
<View>
<Text>Hello World</span>
<Image source={{ uri: '...' }} />
</View>
);
}
});
const { Platform } = require('react-native');
if (Platform.OS === 'ios') {
// runs on ios..
}
if (Platform.OS === 'android') {
// runs on android..
}
/**
* requires ./App.android.js on Android
* requires ./App.ios.js on iOS
*/
const App = require('./App.js');
// React.js
const Component = React.createClass({
render() {
return (
<div style={styles.container}>
<span style={styles.text}>Hello</span>
</div>
);
}
});
const styles = {
container: {
flex: 1
backgroundColor: 'red',
},
text: {
color: 'green'
}
};
// React Native
const Component = React.createClass({
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello</Text>
</View>
);
}
});
const styles = StyleSheet.create({
container: {
flex: 1
backgroundColor: 'red',
},
text: {
color: 'green'
}
});
/**
* Need this to make socket.io
* work in production.
*
* JSCore is running on device
* which has no window.navigator.userAgent,
* but when you run in debug mode
* everythings runs in Chrome (V8)
* which does have window.navigator.userAgent
*/
if (!window.navigator.userAgent) {
window.navigator.userAgent = 'react-native';
}
By Eirik Langholm Vullum
Introduction on building mobile apps with React Native - a web developer perspective.