How it works...

As you can see, creating a new React Native application is very easy but there are some key differences between React (using JSX) and React Native using a special markup with object styles even there are some limitations on the styles as well, for example, let's create a flex layout:

    // Dependencies
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

class Home extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>Header</Text>
</View>

<View style={styles.columns}>
<View style={styles.column1}>
<Text style={styles.column1Text}>Column 1</Text>
</View>

<View style={styles.column2}>
<Text style={styles.column2Text}>Column 2</Text>
</View>

<View style={styles.column3}>
<Text style={styles.column3Text}>Column 3</Text>
</View>
</View>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
height: 100
},
header: {
flex: 1,
backgroundColor: 'green',
justifyContent: 'center',
alignItems: 'center'
},
headerText: {
color: 'white'
},
columns: {
flex: 1
},
column1: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red'
},
column1Text: {
color: 'white'
},
column2: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'blue'
},
column2Text: {
color: 'white'
},
column3: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'orange'
},
column3Text: {
color: 'white'
},
});

export default Home;
File: components/Home.js

You probably don't like looking at a huge file (me neither), so let's separate our component and our styles:

  import { StyleSheet } from 'react-native';

export default StyleSheet.create({
container: {
flex: 1,
height: 100
},
header: {
flex: 1,
backgroundColor: 'green',
justifyContent: 'center',
alignItems: 'center'
},
headerText: {
color: 'white'
},
columns: {
flex: 1
},
column1: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red'
},
column1Text: {
color: 'white'
},
column2: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'blue'
},
column2Text: {
color: 'white'
},
column3: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'orange'
},
column3Text: {
color: 'white'
},
});
File: components/HomeStyles.js

Then in our Home component, we can import the styles and use them in the same way as before:

  // Dependencies
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

// Styles
import styles from './HomeStyles';
...
File: components/Home.js

Here is the result of the code:

But there is something unusual.

As you can see, I created styles for the <Text> components (headerText, column1Text, and so on), and this is because some styles are not allowed in the View component. For example, if you try to add the color: 'white' property to the <View> component, you will see that the property won't work and Header will have the black text:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset