In this lesson, we will create our first React Native application: a simple "Hello World" app. This will help you get familiar with the basic structure of a React Native project and understand how to run your app on a simulator or a real device.
Objectives
- Understand the basic structure of a React Native project.
- Learn how to create a new React Native project.
- Write and run a simple "Hello World" app.
Prerequisites
- Ensure you have set up your development environment as described in the previous lesson.
Creating a New React Native Project
To create a new React Native project, follow these steps:
-
Open your terminal and navigate to the directory where you want to create your project.
-
Run the following command to create a new React Native project:
npx react-native init HelloWorldApp
This command will create a new directory called
HelloWorldApp
with all the necessary files and dependencies. -
Navigate into your project directory:
cd HelloWorldApp
Understanding the Project Structure
Here is a brief overview of the key files and directories in a new React Native project:
node_modules/
: Contains all the project dependencies.ios/
: Contains the iOS-specific code and configuration.android/
: Contains the Android-specific code and configuration.App.js
: The main entry point of your application.index.js
: The entry point for the React Native application.
Writing the "Hello World" App
-
Open the
App.js
file in your code editor. You will see the following default code:import React from 'react'; import { SafeAreaView, StyleSheet, Text, View } from 'react-native'; const App = () => { return ( <SafeAreaView> <View style={styles.container}> <Text style={styles.text}>Welcome to React Native</Text> </View> </SafeAreaView> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, text: { fontSize: 20, }, }); export default App;
-
Modify the code to display "Hello World" instead of "Welcome to React Native":
import React from 'react'; import { SafeAreaView, StyleSheet, Text, View } from 'react-native'; const App = () => { return ( <SafeAreaView> <View style={styles.container}> <Text style={styles.text}>Hello World</Text> </View> </SafeAreaView> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, text: { fontSize: 20, }, }); export default App;
-
Save the file.
Running the App
On iOS Simulator
-
Open your terminal and navigate to your project directory.
-
Run the following command to start the Metro bundler:
npx react-native start
-
Open a new terminal window and navigate to your project directory.
-
Run the following command to start the iOS simulator:
npx react-native run-ios
On Android Emulator
-
Open your terminal and navigate to your project directory.
-
Run the following command to start the Metro bundler:
npx react-native start
-
Open a new terminal window and navigate to your project directory.
-
Run the following command to start the Android emulator:
npx react-native run-android
On a Real Device
- Connect your device to your computer via USB.
- Enable developer mode and USB debugging on your device.
- Follow the same steps as for the emulator, but ensure your device is selected as the target.
Practical Exercise
Exercise 1: Modify the Text
- Change the text from "Hello World" to your name.
- Change the font size to 30.
Solution
-
Modify the
App.js
file:import React from 'react'; import { SafeAreaView, StyleSheet, Text, View } from 'react-native'; const App = () => { return ( <SafeAreaView> <View style={styles.container}> <Text style={styles.text}>Hello, [Your Name]</Text> </View> </SafeAreaView> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, text: { fontSize: 30, }, }); export default App;
-
Save the file and run the app again.
Common Mistakes and Tips
- Ensure your development environment is set up correctly. Refer to the previous lesson if you encounter issues.
- Check for typos in your code, especially in import statements and component names.
- Make sure the Metro bundler is running before you try to run your app on a simulator or device.
Conclusion
Congratulations! You have successfully created and run your first React Native app. In this lesson, you learned how to set up a new project, understand the basic project structure, and write a simple "Hello World" app. This foundational knowledge will be crucial as you progress through the course and build more complex applications.
React Native Course
Module 1: Introduction to React Native
- What is React Native?
- Setting Up the Development Environment
- Hello World App
- Understanding JSX
- Components and Props
Module 2: Core Components and Styling
- Core Components Overview
- Text, View, and Image
- Styling with Flexbox
- Handling User Input
- ScrollView and ListView
Module 3: State and Lifecycle
- State and Lifecycle Methods
- Handling Events
- Conditional Rendering
- Lists and Keys
- Forms and Controlled Components
Module 4: Navigation
- Introduction to React Navigation
- Stack Navigator
- Tab Navigator
- Drawer Navigator
- Passing Parameters to Routes
Module 5: Networking and Data
- Fetching Data with Fetch API
- Using Axios for HTTP Requests
- Handling Network Errors
- AsyncStorage for Local Data
- Integrating with REST APIs
Module 6: Advanced Concepts
Module 7: Deployment and Publishing
- Building for iOS
- Building for Android
- Publishing to App Store
- Publishing to Google Play
- Continuous Integration and Delivery