Updated 25 October 2024
React Native allows us to create mobile apps using JavaScript, which is compatible with both Android and iOS platforms.
This framework not only saves development time by enabling us to code once for both platforms, but it also enhances the overall efficiency of the development process.
Moreover, React Native utilizes native components instead of web components, which leads to improved performance and a more seamless user experience.
In this context, it’s important to note that components serve as the basic building blocks of any application.
In this post, we’ll explore fundamental React Native components. Understanding these will provide you with a solid foundation for effectively building your mobile applications.
So, let’s delve into the key components we are going to study.
1. View
2. Text
3. Image
4. TextInput
5. ScrollView
In React native View is a container and most fundamental component. The view is just like a <div> element in HTML.
It can have nested views inside it and can also have 0 to many children of any type.
1 |
<View>this is view</View> // we can't write text here. |
Let’s take an example of view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import React from "react"; import { View, Text } from "react-native"; const exampleOfView = () => { return ( <View//==>this view is main/Parent container style={{//you can change style accordingly flexDirection: "column",//you can change this to "row" height: 150, padding: 15 }} > <View style={{ backgroundColor: "green", flex: 0.4 }} /> <View style={{ backgroundColor: "yellow", flex: 0.6 }} /> <Text>Example of view</Text> </View> ); }; export default exampleOfView; |
The Text component is used to display text on the page, wrapping the content appropriately. This component <Text>
is similar to the <p />
tag in HTML.
Additionally, we can apply various styles to the text using the style
prop, allowing for customization and enhanced visual appeal.
Here’s an example of the Text component:
1 2 3 4 5 6 7 8 9 10 11 12 |
import React from "react"; import { View, Text } from "react-native"; export default class App extends React.Component { render() { return ( <View> <Text>This is the example of the Text </Text> </View> ); } } |
In React Native, we can display images from various sources, including local disks, network images, static resources, and temporary local files.
This flexibility allows us to effectively showcase images within our app.
The Image component in React Native is similar to the <img />
tag in HTML, making it easy to implement. Furthermore, we can use the BackgroundImage component to set background images on any page.
This component allows us to place any child elements, such as text or additional images, inside it, thereby enhancing the visual design and functionality of our application.
Overall, these image components provide robust options for integrating visuals, contributing significantly to the user experience.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import React from 'react'; import { View, Image, Text, StyleSheet } from 'react-native'; const styles = StyleSheet.create({ container: { flex: 1, // Allow the container to take full height justifyContent: 'center', // Center content vertically alignItems: 'center', // Center content horizontally paddingTop: 40, }, staticSource: { width: 240, height: 220, }, networkSource: { margin: 20, width: 66, height: 58, }, text: { marginTop: 10, fontSize: 16, textAlign: 'center', }, }); const ImageExample = () => { return ( <View style={styles.container}> {/* Static source or image from folder example */} <View style={{ alignItems: 'center' }}> <Image style={styles.staticSource} source={require('./assets/react.png')} // Update the path here /> <Text style={styles.text}>Example of Image View</Text> </View> {/* Image from network or URI example */} <View style={{ alignItems: 'center' }}> <Image style={styles.networkSource} source={{ uri: 'https://example.images/react.png', }} /> </View> </View> ); }; export default ImageExample; |
Taking input from users is the most important part of any application. In React native we can achieve this by using the Text input component.
This is similar to the <input> tag in HTML.
We can use onChangeText to detect the change in input. There are also other events, such as onSubmitEditing and onFocus that can also be used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import React, { useState } from 'react'; import { StyleSheet, Text, TextInput, View, Button, Alert } from 'react-native'; const InputTextExample = () => { const [text, setText] = useState(''); const handleSubmit = () => { if (text.trim() === '') { Alert.alert('Validation', 'Please enter some text!'); } else { Alert.alert('Submitted', `You entered: ${text}`); setText(''); // Clear input after submission } }; return ( <View style={styles.container}> <TextInput style={styles.input} placeholder="Input you text here!!" placeholderTextColor="white" onChangeText={setText} value={text} returnKeyType="done" // Shows "Done" on keyboard onSubmitEditing={handleSubmit} // Submit on pressing "Done" blurOnSubmit // Dismiss keyboard after submission /> <Button title="Submit" onPress={handleSubmit} /> <Text style={styles.output}> {text} </Text> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 16, backgroundColor: '#f9f9f9', }, input: { height: 50, backgroundColor: 'gray', fontSize: 18, paddingHorizontal: 10, marginBottom: 16, borderRadius: 5, // Rounded corners borderWidth: 1, borderColor: '#ccc', // Light border color }, output: { padding: 16, fontSize: 20, color: 'red', }, }); export default InputTextExample; |
As we are all aware, the ScrollView component provides the functionality to scroll through multiple child components and views.
Essentially, the ScrollView establishes scrolling boundaries for the nested children and views on our page.
In React Native, it acts as a container that wraps our nested views, allowing for better organization and usability. Additionally, we can set the scrolling direction to either horizontal or vertical.
By default, the ScrollView scrolls vertically; however, we can easily change this to horizontal by using the horizontal: true
prop.
Overall, the ScrollView is a powerful component that enhances the user experience by allowing smooth navigation through content.
1 2 3 4 5 6 7 8 9 10 11 |
import React from 'react'; import { Image, ScrollView, Text } from 'react-native'; export default App = () => ( <ScrollView> // add your view or content here. </ScrollView> ); |
In this blog, we explored several basic components in React Native, including View, Text, Image, TextInput, and ScrollView.
Each of these components plays a crucial role in building a user-friendly mobile application.
By understanding how to utilize these components effectively, we can create engaging and responsive user interfaces that enhance the overall user experience.
The code snippets provided throughout the blog illustrate how to implement these components in real-world scenarios, showcasing their versatility and functionality.
You can also check other blogs from here.
Thank you for reading! ❤️
I hope this blog has helped you gain a solid understanding of these essential React Native components and inspired you to leverage their capabilities in your own projects.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.