There are certain prerequisites that beginners will need to set up in order to develop for React Native. Since iOS was the first platform supported, and the one we’re covering in this tutorial, we need macOS and Xcode, at least version 6.3. Node.js is also needed. What helps is installing Watchman through the Brew package manager with
brew install watchman. While this is not necessarily needed, it helps when dealing with a lot of files inside our React Native projectTo install React Native, we simply need to install the React Native command-line application with
npm install -g react-native-cli. Calling the react-native command then helps us create a new React Native application. Running react-native init HelloWorld creates a folder called HelloWorld in which the boilerplate code can be found.Transforming a React Application
With React being the key feature and the core principles coming from the React library, let’s take a look at what we need to transform a minimal React “Hello World” application into a React Native one.
We use some ES2015 features in this code example, specifically classes. It is completely feasible to stick with
React.createClass or use a function form similar to the popular module pattern.var React = require('react');
class HelloThere extends React.Component {
clickMe() {
alert('Hi!');
}
render() {
return (
<div className="box" onClick={this.clickMe.bind(this)}>Hello {this.props.name}. Please click me.</div>
);
}
}
React.render(<HelloThere name="Component" />, document.getElementById('content'));
Step 1: Embrace CommonJS Modules
In the first step we need to change requiring the React module to use
react-native instead.var React = require('react-native');
class HelloThere extends React.Component {
clickMe() {
alert('Hi!');
}
render() {
return (
<div className="box" onClick={this.clickMe.bind(this)}>Hello {this.props.name}. Please click me.</div>
);
}
}
React.render(<HelloThere name="Component" />, document.getElementById('content'));
What is usually a part of the tooling pipeline when developing a React web application is an integral part of React Native.
Step 2: There Is No DOM
Not surprisingly, there is no DOM on mobile. Where we previously used
<div />, we need to use <View /> and where we used <span />, the component we need here is <Text />.import React from ‘react';
import {View, Text, Alert} from ‘react-native';
class HelloThere extends React.Component {
clickMe() {
Alert.alert(‘hi!');
}
render() {
return (
<View className="box" onClick={this.clickMe.bind(this)}>Hello {this.props.name}. Please click me.</View>
);
}
}
React.render(<HelloThere name="Component" />, document.getElementById('content'));
While it’s quite convenient to put text directly in
<div /> elements, in the native world text can’t be put directly in a <View />. For that we need to insert a <Text /> component.import React from ‘react';
import {View, Text, Alert} from ‘react-native';
class HelloThere extends React.Component {
clickMe() {
Alert.alert(‘hi!');
}
render() {
return (
<View className="box" onClick={this.clickMe.bind(this)}>
<Text>Hello {this.props.name}. Please click me.</Text>
</View>
);
}
}
React.render(<HelloThere name="Component" />, document.getElementById('content'));
Step 3: Inline Styles Are the Way to Go
React Native allows us to use the Flexbox modeling instead of messing around with
float and inline-block that we are so familiar with in the web world. The interesting thing is that React Native does not use CSS.import React from ‘react';
import {View, Text, StyleSheet, Alert} from ‘react-native';
class HelloThere extends React.Component {
clickMe() {
Alert.alert(‘hi!');
}
render() {
return (
<View style={styles.box} onClick={this.clickMe.bind(this)}>
<Text>Hello {this.props.name}. Please click me.</Text>
</View>
);
}
}
var styles = StyleSheet.create({
box: {
borderColor: 'red',
backgroundColor: '#fff',
borderWidth: 1,
padding: 10,
width: 100,
height: 100
}
});
React.render(<HelloThere name="Component" />, document.getElementById('content'));
Using inline styles seems bewildering to beginners. It is similar to the transition React developers had to go through when being confronted with JSX and previously using templating engines like Handlebars or Jade.
The idea is that we don’t have stylesheets globally in the way we use CSS. We declare the stylesheets directly at component level, and so we have all the information we need to see what our component does, the layout it creates, and the styles it applies.
import React from ‘react';
import {Text} from ‘react-native';
var Headline = function(props) {
this.render = () => <Text style={headlineStyle.text}>{props.caption}</Text>;
};
var headlineStyles = StyleSheet.create({
text: {
fontSize: 32,
fontWeight: 'bold'
}
});
module.exports = Headline;
Step 4: Handling Events
The equivalent to clicking in web pages is tapping an element on the mobile device. Let’s change our code so that the “alert” pops up when we tap the element.
import React from ‘react';
import {View, Text, StyleSheet, TouchableOpacity, Alert} from ‘react-native';
class HelloThere extends React.Component {
clickMe() {
Alert.alert("Hi!")
}
render() {
return (
<TouchableOpacity onPress={this.clickMe()}>
<View style={styles.box}>
<Text>Hello {this.props.name}. Please click me.</Text>
</View>
</TouchableOpacity>
);
}
}
var styles = StyleSheet.create({
box: {
borderColor: 'red',
backgroundColor: '#fff',
borderWidth: 1,
padding: 10,
width: 100,
height: 100
}
});
React.render(<HelloThere name="Component" />, document.getElementById('content'));
Instead of events being directly available on
<View /> components, we need to explicitly use elements that trigger events, in our case a touch event when pressing the view. There are different types of touchable components available, each of them providing a different visual feedback.
No comments:
Post a Comment