What is React Native?
React Native is a framework for building mobile applications using JavaScript and React.
It allows you to write mobile applications that run on iOS and Android using a single codebase.
Try changing the code below to see how it affects the app:
1. The View
Component
Section titled “1. The View Component”Understanding View
Section titled “Understanding View”The View
component is a fundamental building block in React Native.
It is a container that supports layout with flexbox, style, touch handling, and accessibility controls.
Creating a Simple View
Section titled “Creating a Simple View”Replace the code in the Snack editor with the following to create a simple View
component:
import React from 'react';import { View, StyleSheet } from 'react-native';
const ViewExample = () => { return ( <View style={styles.container}> <View style={styles.box} /> </View> );};
const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, box: { width: 100, height: 100, backgroundColor: 'blue', },});
export default ViewExample;
Look carefully at how the styles
object is used to style the View
components.
2: Introduction to Text Component
Section titled “2: Introduction to Text Component”Understanding Text
Section titled “Understanding Text”The Text component is used to display text in your app. It supports nesting, styling, and touch handling.
Creating a Simple Text
Section titled “Creating a Simple Text”Replace the code in your Snack editor with the following to create a simple Text component:
import { View, Text, StyleSheet } from 'react-native';
const TextExample = () => { 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: 'black', },});
export default TextExample;
Again, look carefully at how the styles
object is used to style the Text
component.
3: Handling Text Input in React Native
Section titled “3: Handling Text Input in React Native”Understanding TextInput
Section titled “Understanding TextInput”The TextInput
component is used to capture text input from the user.
It supports various properties to handle user input, styling, and events. Details about the TextInput
component can be found in the official documentation.
Creating a Simple TextInput
Section titled “Creating a Simple TextInput”Replace the code in your Snack editor with the following to create a simple TextInput
component:
import React, { useState } from 'react';import { View, TextInput, Text, StyleSheet } from 'react-native';
const TextInputExample = () => { const [text, setText] = useState('');
return ( <View style={styles.container}> <TextInput style={styles.input} placeholder="Type here..." onChangeText={setText} value={text} /> <Text style={styles.text}>You typed: {text}</Text> </View> );};
const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 16, }, input: { height: 40, borderColor: 'gray', borderWidth: 1, paddingHorizontal: 8, width: '80%', marginBottom: 12, }, text: { fontSize: 18, },});
export default TextInputExample;
Look carefully at how the TextInput
component is used to capture and display user input.
The useState
hook is used to manage the input value. The onChangeText
prop is used to update the input value when the user types.
More React Native Components
Section titled “More React Native Components”React Native provides a wide range of components to build mobile applications. Some commonly used components include:
Button
: A button component to handle user interactions.Switch
: A switch component to toggle between two states.Image
: An image component to display images.ScrollView
: A scrollable container to display content.FlatList
: A component to render lists efficiently.Modal
: A modal component to display content on top of other content.Pressable
: A component to handle press interactions.
Visit each of the links above and explore these components further.
Platform-Specific Components
Section titled “Platform-Specific Components”React Native also provides platform-specific components like:
SafeAreaView
: A component to handle safe area insets on iOS.
or
DrawerLayoutAndroid
: A component to create a drawer layout on Android.