How to do it...

Now, to create our first app:

  1. Let's do it with this command:
    react-native init MyFirstReactNativeApp
  1. After we built our React Native app, we need to install Watchman, which is a file-watching service required by React Native. To install it, go to https://facebook.github.io/watchman/docs/install.html and download the latest version for your OS (Windows, Mac, or Linux).
  2. In this case, we are going to use Homebrew to install it for Mac. If you don't have Homebrew, you can install it with this command:
    /usr/bin/ruby -e "$(curl -fsSL 
https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. To install Watchman, you need to run:
    brew update 
brew install watchman
  1. To start the React Native project, we need to use:
    react-native start

  1. If everything works fine, you should see this:

Sometimes you can get errors from Watchman, for example,
Watchman error: too many pending cache jobs. Make sure watchman is running for this project.

If you get that error or another, you have to uninstall Watchman by doing:

brew unlink watchman

And then reinstall it using:

brew update && brew upgrade

brew install watchman

  1. Open a new terminal (Cmd + T) and run this command (depending on the device you want to use):
    react-native run-ios 
or
react-native run-android

  1. If there are no errors, you should see the simulator running the default application:

Now that we have our application running, let's open our code and modify it a bit:

  1. Change the App.js file:
  ...
export default class
App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
This is my first React Native App!
</Text>
<Text style={styles.instructions}>
To get started, edit App.js
</Text>
<Text style={styles.instructions}>{instructions}</Text>
</View>
);
}
}
...
File: App.js
  1. If you go to the simulator again, you will need to press Cmd + R to reload the app to see the new changes reflected:

  1. You're probably wondering if there is a way to do automatic reload instead of doing this process manually, and of course, there is a way to enable the Live Reload option; you need to press Cmd + D to open the development menu and then select the Enable Live Reload option:

  1. Another exciting option is Debug JS Remotely. If you click on that one, it will automatically open a Chrome tab where we can see the logs we added to our application using console.log. For example, if I add console.log('==== Debugging my First React Native App! ===='); in my render method, I should see it like this:

  1. Let's go back to the code. Maybe you are a little bit confused about the code you saw in App.js because you didn't see a <div> tag or even worse the way the styles are being created like an object instead of using a CSS file as we do in React. I've got some good news and some bad news; the bad news is that React Native does not support CSS and JSX/HTML code as React does. The good news is that once you understand that the <View> component is the equivalent of using a <div>, <Text> is the equivalent of using <p>and the styles are like CSS modules (object), everything else works the same as React (props, state, lifecycle methods). 
  2. Create a new component (Home). For this purpose, we have to create a directory called components, and then we save this file as Home.js:
  // Dependencies
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

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

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
home: {
fontSize: 20,
textAlign: 'center',
margin: 10,
}
});

export default Home;
File: components/Home.js
  1. In App.jswe import the Home component, and we render it:
  // Dependencies
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';

// Components
import Home from './components/Home';

class App extends Component {
render() {
return (
<Home />
);
}
}

export default App;
File: App.js
..................Content has been hidden....................

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