React Native is a mobile development that’s based on React that we can use to do mobile development.
In this article, we’ll look at how to use it to create an app with React Native.
Modal
We can add a modal to present content above the enclosing view.
For example, we can write:
import React, { useState } from 'react';
import {
Alert,
Modal,
StyleSheet,
Text,
TouchableHighlight,
View
} from "react-native";
export default function App() {
const [modalVisible, setModalVisible] = useState(false);
return (
<View style={styles.centeredView}>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert("Modal has been closed.");
}}
>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<TouchableHighlight
style={styles.openButton}
onPress={() => {
setModalVisible(!modalVisible);
}}
>
<Text style={styles.textStyle}>Hide Modal</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
<TouchableHighlight
style={styles.openButton}
onPress={() => {
setModalVisible(true);
}}
>
<Text style={styles.textStyle}>Show Modal</Text>
</TouchableHighlight>
</View>
);
}
const styles = StyleSheet.create({
centeredView: {
flex: 1,
justifyContent: "center",
alignItems: "center",
marginTop: 22
},
modalView: {
margin: 20,
backgroundColor: "white",
borderRadius: 20,
padding: 35,
alignItems: "center",
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 2
},
shadowOpacity: 0.25,
shadowRadius: 3.84,
elevation: 5
},
openButton: {
backgroundColor: "pink",
borderRadius: 20,
padding: 10,
elevation: 2
},
textStyle: {
color: "white",
fontWeight: "bold",
textAlign: "center"
},
modalText: {
marginBottom: 15,
textAlign: "center"
}
});
We add the View to enclose a Modal component with the modal.
The modal has the View to hold the content.
The TouchableHighlight component is a button that lets us press it to close the modal.
The modal’s open state is opened by the modalVisible state.
The visible prop in the Modal sets the open state.
Below the Modal , we have another TouchableHighlight component to call setModalVisible with true to open it.
PixelRatio
The PixelRatio object gives us access to the device’s pixel density and the font scale.
We can use it to correctly size an image.
For example, we can write:
import React from 'react';
import { Image, PixelRatio, StyleSheet, View } from "react-native";
const size = 50;
const cat = {
uri: "https://reactnative.dev/docs/assets/p_cat1.png",
width: size,
height: size
};
export default function App() {
return (
<View style={styles.container}>
<Image
source={cat}
style={{
width: PixelRatio.getPixelSizeForLayoutSize(size),
height: PixelRatio.getPixelSizeForLayoutSize(size)
}}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
justifyContent: "center",
alignItems: "center"
},
value: {
fontSize: 24,
marginBottom: 12,
marginTop: 4
}
});
We set the width and height in the Image component by calling PixelRatio.getPixelSizeForLayoutSize .
The pixel size will be rounded when we specify size with arbitrary precision.
Therefore, we’ve to be careful with rounding errors.
RefreshControl
We can add a RefreshControl component inside a ScrollView or ListView to add pull to refresh functionality.
For example, we can write:
import React from 'react';
import {
ScrollView,
RefreshControl,
StyleSheet,
Text,
SafeAreaView,
} from 'react-native';
import Constants from 'expo-constants';
const wait = (timeout) => {
return new Promise(resolve => {
setTimeout(resolve, timeout);
});
}
export default function App() {
const [refreshing, setRefreshing] = React.useState(false);
const onRefresh = React.useCallback(() => {
setRefreshing(true);
wait(2000).then(() => setRefreshing(false));
}, []);
return (
<SafeAreaView style={styles.container}>
<ScrollView
contentContainerStyle={styles.scrollView}
refreshControl={
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
}
>
<Text>Pull to Refresh</Text>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
},
scrollView: {
flex: 1,
backgroundColor: 'pink',
alignItems: 'center',
justifyContent: 'center',
},
});
We added the RefreshControl in the ScrollView to add the pull to refresh indicator that shows when we pull the screen down.
The refreshing prop controls when the RefreshControl is shown.
And onRefresh is run when we show the RefreshControl .
Conclusion
We can add a modal and refresh indicator into our React Native app.
Also, we can size image proportionally with the PixelRatio object.