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 Reac.
Uri Data Images
We can add images for a base64 URI.
For example, we can write:
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { Image, StyleSheet, View } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<Image
style={{
width: 51,
height: 51,
resizeMode: 'contain'
}}
source={{
uri:
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='
}}
/>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
We just set the uri
property to set the image URI.
We should only use this for small images.
Background Image
For example, we can write:
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { ImageBackground, StyleSheet, View, Text } from 'react-native';
export default function App() {
return (
<View style={styles.container}>
<ImageBackground source={require('./assets/fork.jpg')} style={{ width: '100%', height: '100%' }}>
<Text>Inside</Text>
</ImageBackground>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
We used the ImageBackground
component to add the background image.
Then we can have things like text inside it.
View
A View
is a container that supports layouts with flexbox, styles, touch handling, and accessibility controls.
For example, we can write:
import React from 'react';
import { View, Text } from 'react-native';
export default function App() {
return (
<View
style={{
flexDirection: "row",
height: 100,
padding: 20
}}
>
<View style={{ backgroundColor: "green", flex: 0.3 }} />
<View style={{ backgroundColor: "red", flex: 0.5 }} />
<Text>Hello World!</Text>
</View>
);
}
The outer View
component is a flexbox container for the inner View
components.
We can set the backgroundColor
for views.
And we can add the Text
component to add the text.
Text
The Text
component is a component that’s used for displaying text.
For example, we can write:
import React, { useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
const onPressTitle = () => {
console.log("title pressed");
};
export default function App() {
const titleText = useState("Bird's Nest");
const bodyText = useState("This is not really a bird nest.");
return (
<View
style={{
flexDirection: "row",
height: 100,
padding: 20
}}
>
<Text style={styles.baseText}>
<Text style={styles.titleText} onPress={onPressTitle}>
{titleText}
{"n"}
</Text>
<Text numberOfLines={5}>{bodyText}</Text>
</Text>
</View>
);
}
const styles = StyleSheet.create({
baseText: {
fontFamily: "Cochin"
},
titleText: {
fontSize: 20,
fontWeight: "bold"
}
});
We can add the Text
component to add some text.
We add the onPress
prop and pass in an event handler to add an event handling for touching the text.
Conclusion
We can add URIs for images and background images with React Native.
Also, we can add View
components for containers and Text
component for text.