Chapter 1: Introduction to React Native and Expo
1. What is React Native?
React Native is a framework for building native applications using React. It allows developers to create mobile apps using JavaScript and React, but it compiles to native code, providing a more native-like experience.
Key Points:
- Cross-Platform Development: Write once, run on both iOS and Android.
- Native Components: React Native uses native components for a more native-like experience compared to hybrid frameworks.
- Performance: Offers near-native performance by using native components.
Example: Creating a basic React Native component.
// App.js
import React from "react";
import { View, Text, StyleSheet } from "react-native";
const App = () => {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello, React Native!</Text>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
text: {
fontSize: 20,
color: "blue",
},
});
export default App;
2. What is Expo and Why Use It?
Expo is an open-source platform that builds on top of React Native. It provides tools and services to streamline the development and deployment process.
Benefits of Using Expo:
- Ease of Setup: Simplifies the setup and configuration of React Native projects.
- Pre-built Components: Includes a set of components and APIs that enhance functionality.
- Over-the-Air Updates: Allows for easy updates without requiring users to download a new version from the app store.
- Expo Go App: Helps in testing the app on a real device without needing a full build setup.
Example: Creating a basic Expo project and running it.
-
Install Expo CLI:
npm install -g expo-cli -
Create a New Expo Project:
expo init MyNewProject -
Navigate to the Project Directory and Run the Project:
cd MyNewProject expo start -
Example App (App.js):
import React from "react"; import { Text, View, StyleSheet } from "react-native"; export default function App() { return ( <View style={styles.container}> <Text style={styles.text}>Welcome to Expo!</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center", alignItems: "center", }, text: { fontSize: 20, }, });
3. Overview of React Native Architecture
React Native Architecture is based on a bridge between JavaScript and native code. Here’s a simplified view of its components:
- JavaScript Code: Your application logic, written in JavaScript.
- React Native Bridge: A bridge that allows communication between JavaScript and native modules.
- Native Modules: Native code that provides platform-specific functionalities (e.g., accessing the camera, GPS).
- UI Layer: The React Native view hierarchy that gets rendered on the device.
Example: How the architecture translates to a simple React Native component.
- JavaScript Code interacts with React Native Bridge.
- The Bridge calls Native Modules to access device features.
- The UI Layer displays the content based on the data provided.
Diagram:
JavaScript Code <--> React Native Bridge <--> Native Modules <--> UI Layer
4. Understanding the Differences between React and React Native
React is used for building web applications, while React Native is used for mobile applications. Here are key differences:
-
Rendering:
- React: Renders to HTML elements in the browser.
- React Native: Renders to native mobile components.
-
Components:
- React: Uses HTML elements like
<div>,<span>, etc. - React Native: Uses native components like
<View>,<Text>,<Image>.
- React: Uses HTML elements like
-
Styling:
- React: Uses CSS or pre-processors like SASS.
- React Native: Uses JavaScript styling with
StyleSheet.create.
-
Navigation:
- React: Uses libraries like React Router.
- React Native: Uses navigation libraries like React Navigation or Expo’s file-based routing.
Example: A simple button component in both React and React Native.
-
React (Web):
// ReactButton.js import React from "react"; const ReactButton = () => { return ( <button style={{ padding: "10px", backgroundColor: "blue", color: "white" }} > Click Me </button> ); }; export default ReactButton; -
React Native (Mobile):
// ReactNativeButton.js import React from "react"; import { TouchableOpacity, Text, StyleSheet } from "react-native"; const ReactNativeButton = () => { return ( <TouchableOpacity style={styles.button}> <Text style={styles.text}>Click Me</Text> </TouchableOpacity> ); }; const styles = StyleSheet.create({ button: { padding: 10, backgroundColor: "blue", borderRadius: 5, }, text: { color: "white", textAlign: "center", }, }); export default ReactNativeButton;
This chapter gives you a foundational understanding of React Native and Expo, including their differences and how they work together. For the latest examples, ensure you are using the most recent versions of libraries and tools as of 2024.