visit
This tutorial is the third part of our Airbnb Home Screen UI clone using React Native. In the previous part, we successfully implemented the Category and Airbnb plus sections. This tutorial is the continuation of the same tutorial from where we left off in the last part. So, it is recommended to go through the previous parts for better understanding and insight into the overall project.
As mentioned in the previous parts, this tutorial series was inspired by React native template which helps us build some amazing ready to deploy applications that anyone can use to build startups or sell the application templates. And, this third part is also the continuation of coding implementations and designs from the Youtube video tutorial by for the Airbnb clone.This part of our tutorial series is fairly simple. Here, we are going to implement a section that displays the home packages in the Home Screen UI. The idea is to include an image with some description, title, and price in the grid-style layout. Then, we are also going to include the star-ratings from the package later on.So, let us begin!!
First, we are going to add a title to the home package section. For that, we need to make use of
Text
component with some inline styles in our Explore.js file just below the Airbnb Plus section. Then, we need to wrap the
Text
component with the View component with some margin style as shown in the code snippet below:<View style={{ marginTop: 40 }}>
<Text
style={{
fontSize: 24,
fontWeight: "700",
paddingHorizontal: 20
}}
>
Homes around the world
</Text>
Now, we need to add an image and its description just below the title section that we just implemented. For the image, we are going to use the
Dimensions
component provided by the react-native package. The Dimensions
component enables us to get the full width and height of our app screen. So, we need to import the Dimensions
component along with other components from the react-native package. And then, we need to assign two constants height and width which are initialized to the entire height and width of the screen using get() method provided by
Dimentions
component as shown in the code snippet below:import {
View,
TextInput,
Text,
Image,
SafeAreaView,
ScrollView,
Dimensions
} from "react-native";
import Category from "../components/Category";
const { height, width } = Dimensions.get("window");
The Image component is bound to its own inline style properties as shown in the code snippet below:
<View style={{ paddingHorizontal: 20, marginTop: 20 }}>
<View style={{ width: width / 2, height: width / 3 }}>
<Image
style={{
flex: 1,
width: null,
height: null,
resizeMode: "cover"
}}
source={require("../images/home.jpeg")}
/>
</View>
<View style={{ flex: 1 }}></View>
</View>
Now, it is time to add the description section just below the image. We have defined a child View component already for this section. Now, we are going to add three
Text
components inside this View component, all with their own inline style properties. The View component itself has its own set of inline style properties which includes flex styles as shown in the code snippet below:<View
style={{
flex: 1,
alignItems: "flex-start",
justifyContent: "space-evenly",
paddingLeft: 10
}}
>
<Text style={{ fontSize: 10, color: "#b63838" }}>
Private Roome - 4 Beds
</Text>
<Text style={{ fontSize: 12, fontWeight: "bold" }}>
The Spring House
</Text>
<Text style={{ fontSize: 10 }}>65$</Text>
</View>
‘./components’
directory.Home
which extends to Component module. The props we need to send from parent component and receive here in this Home.js child component are height and with properties, type, name, and price as shown in the code snippet below:
import React, { Component } from "react";
import { View, Text, StyleSheet, Image } from "react-native";
class Home extends Component {
render() {
return (
<View
style={{
width: this.props.width / 2 - 30,
height: this.props.width / 2 - 30,
borderWidth: 0.5,
borderColor: "#dddddd"
}}
>
<View style={{ flex: 1 }}>
<Image
style={{ flex: 1, width: null, height: null, resizeMode: "cover" }}
source={require("../images/home.jpeg")}
/>
</View>
<View
style={{
flex: 1,
alignItems: "flex-start",
justifyContent: "space-evenly",
paddingLeft: 10
}}
>
<Text style={{ fontSize: 10, color: "#b63838" }}>
{this.props.type}
</Text>
<Text style={{ fontSize: 12, fontWeight: "bold" }}>
{this.props.name}
</Text>
<Text style={{ fontSize: 10 }}>{this.props.price}$</Text>
</View>
</View>
);
}
}
export default Home;
<View
style={{
padding: 20,
marginTop: 20,
flexDirection: "row",
flexWrap: "wrap",
justifyContent: "space-between"
}}
>
<Home
width={width}
name="The Cozy Place"
type="PRIVATE ROOM - 2 BEDS"
price={82}
/>
<Home
width={width}
name="The Cozy Place"
type="PRIVATE ROOM - 2 BEDS"
price={82}
/>
<Home
width={width}
name="The Cozy Place"
type="PRIVATE ROOM - 2 BEDS"
price={82}
/>
</View>
yarn add react-native-star-rating
Next, we need to import the react-native-star-rating package into our Home.js file component as shown in the code snippet below:
import { View, Text, StyleSheet, Image } from "react-native";
import StarRating from 'react-native-star-rating'
class Home extends Component {
Then, we need to initialize the
StarRating
component that we imported just below the Text component featuring price of the package as shown in the code snippet below: <View style={{ flex: 1, alignItems: 'flex-start', justifyContent: 'space-evenly', paddingLeft: 10 }}>
<Text style={{ fontSize: 10, color: '#b63838' }}>{this.props.type}</Text>
<Text style={{ fontSize: 12, fontWeight: 'bold' }}>{this.props.name}</Text>
<Text style={{ fontSize: 10 }}>{this.props.price}$</Text>
<StarRating
disable={true}
maxStars={5}
rating={this.props.rating}
starSize={10}
/>
</View>
The disable prop has the value of true in order to make the stars read-only. maxStars prop has a value of 5 in order to display a total of 5 stars only.The ratings prop is set to the prop value that is to be received from the parent component. This rating prop is used to display the number of stars that a home package has among a total of 5 stars.
Now, we need to go to the Explore.js file and set another prop to our Home component. The rating prop with required value needs to be passed down to the Home child component as shown in the code snippet below:<Home
width={width}
name="The Cozy Place"
type="PRIVATE ROOM - 2 BEDS"
price={82}
rating={4}
/>
<Home
width={width}
name="The Cozy Place"
type="PRIVATE ROOM - 2 BEDS"
price={82}
rating={3}
/>
<Home
width={width}
name="The Cozy Place"
type="PRIVATE ROOM - 2 BEDS"
price={82}
rating={5}
/>
We also learned how to arrange the home package components in grid style making the use of
Dimensions
component. Then, we got an insight into how to use the react-native-star-rating package in order to display the star ratings into our home packages. And then, we finally displayed the Home Packages Section to our Home Screen UI clone using React Native. In the next part, we will start implementing animations of the search bar.