visit
This tutorial is the second part of our React Native Car Parking App UI clone series. In the previous part, we successfully implemented the MapView section as well as separated different UI sections for the map screen. 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 part in order to establish the basis and get insight into the overall project.
As mentioned in the previous part, the motivation to implement this UI clone series came from the React Native App Templates that provides us with a dynamic, fully-coded starter kit written in React Native that anyone can use to build their own store locator React Native application or initiate their own startup. And, this second part is also the continuation of coding implementations and designs from the Youtube video tutorial by for the Camping Spots Finder App clone. The video tutorial provides overall guidance using fast coding which may be difficult to grasp for any developer especially the beginners. This written tutorial provides the step by step implementation which will be easier to understand and implement.So, let us begin!!
Here, we are going to define the mock data array which will contain the parking spot information as key-value pair in the object. Hence, we are going to initialize an array of objects in the Map.js file.
So, we are going to initialize an array constant called
parkingsSpots
with some parking spot data as shown in the code snippet below:const parkingsSpots = [
{
id: 1,
title: 'Parking 1',
price: 5,
rating: 4.2,
spots: 20,
free: 10
},
{
id: 2,
title: 'Parking 2',
price: 7,
rating: 3.8,
spots: 25,
free: 20
},
{
id: 3,
title: 'Parking 3',
price: 10,
rating: 4.9,
spots: 50,
free: 25
},
];
Now, we are going to integrate this data into our parking section by using it in our renderParkings() and renderParking() functions that we have defined in the earlier part of the tutorial. Here in the renderParkings() method, we are going to use map() array function in order to iterate through each item in the
parkingsSpots
array. Then, the map() function will call the renderParking() function by sending a parameter which is an item of the
parkingsSpots
array as shown in the code snippet below:renderParkings(){
return(
<ScrollView horizontal contentContainerStyle={styles.parkings}>
{parkingsSpots.map((parking) => this.renderParking(parking))}
</ScrollView>
)
}
Now, the renderParking() method is going to receive an item of
parkingsSpots
array as a parameter. Then, we are going to use the parameter item in order to return the template with parking spot data as shown in the code snippet below:renderParking(item){
return(
<View key={`parking-${item.id}`} style={styles.parking}>
<Text>{item.title}</Text>
</View>
)
}
Here, we have used the item
id
as a key in order to identify each View
component of a particular parking spot uniquely. Then, for now, we have just used the title information from the parking data.Hence, we will get the following result in our emulator screen:Now, we are going to begin by adding the
Dimensions
component to our Map
screen. The Dimensions
component enables us to get the height and width of the entire app screen. For that, we need to import the
Dimensions
component from the react-native package as shown in the code snippet below:import { StyleSheet, Text, View, ScrollView, Dimensions } from 'react-native';
Then, we need to define two constants for height and width. After that, we need to use the get() method with parameter as ‘screen’ in order to get the full width and height of the app screen. The code for this is provided in the code snippet below:
const {height , width} = Dimensions.get('screen');
Now, we are going to make some changes to the renderParkings() method as shown in the code snippet below:
renderParkings(){
return(
<ScrollView horizontal style={styles.parkings}>
{parkingsSpots.map(parking => this.renderParking(parking))}
</ScrollView>
)
}
Here, we have replaced the
contentContainerStyle
prop with just style prop. Then, we need to make some configurations to parking style variable as shown in the code snippet below:parking : {
backgroundColor : 'white',
borderRadius : 6,
padding : 12,
marginHorizontal: 24,
width : width - ( 24 * 2 )
}
Here, we have added the width property to parking style with its value initialized to value from
Dimensions
component and then some subtraction of the total width.Hence, we will get the following result in our emulator screen:As we can see, the parking section appears as cards with parking data. And, the cards are horizontally scrollable as well. The
MapView
also covers the entire lower section and the parking section overlaps the lower MapView
section.But, the scrolling action does not look ideal here. What we want is to show only one parking spot card when we perform the scrolling or swiping action. So, we need to make some additional configuration in order to achieve that.Additional configuration for smooth Swiping Action
Now, we are going to make some additional configuration to our parking section cards in order to make the transition of parking spot cards smooth. And, we also need to make only one parking spot card visible at a time.For that, we need to add some additional prop to
ScrollView
component in the renderParkings() method as shown in the code snippet below:renderParkings(){
return(
<ScrollView
horizontal
style={styles.parkings}
pagingEnabled
scrollEnabled
>
{parkingsSpots.map(parking => this.renderParking(parking))}
</ScrollView>
)
}
Here, we have added the pagingEnabled and scrollEnabled props to the
ScrollView
component.parkings:{
position: 'absolute',
right: 0,
left: 0,
bottom: 24,
},
Here, we have added the bottom gap of 24 pixels to the parking section.
Hence, we will get the following result in our emulator screen:But, we can see the horizontal scroll bar at the bottom of the parking section. Now, we need to remove that scroll bar. For that, we need to add a prop called showsHorizontalScrollIndicator to the
ScrollView
component of renderParkings() method. The showsHorizontalScrollIndicator
be initialized as false. The coding implementation for this is shown in the code snippet below: <ScrollView
horizontal
style={styles.parkings}
pagingEnabled
scrollEnabled
showsHorizontalScrollIndicator = {false}
>
{parkingsSpots.map(parking => this.renderParking(parking))}
</ScrollView>
Now, in order to make the scrolling more smoother and remove the lagging effect, we need to add some addition prop to control the throttle of
ScrollView
while scrolling. For that, we need to add additional props to the
ScrollView
component of the renderParkings() function as shown in the code snippet below: <ScrollView
horizontal
style={styles.parkings}
pagingEnabled
scrollEnabled
showsHorizontalScrollIndicator = {false}
scrollEventThrottle={16}
snapToAlignment="center"
>
{parkingsSpots.map(parking => this.renderParking(parking))}
</ScrollView>
Here, we have added the scrollEventThrottle and snapToAlignment props. The scrollEventThrottle prop is set to 16 and snapToAlignment prop is set to center.
Adding these two props to
ScrollView
component will make the transition of swiping action more smooth. In addition to this, we are going to increase the padding of our parking spot cards as shown in the code snippet below:parking : {
backgroundColor : 'white',
borderRadius : 6,
padding : 24,
marginHorizontal: 24,
width : width - ( 24 * 2 )
}
This tutorial is the second part of the React Native Car Parking Finder App UI clone tutorial series. In this part, we continued from where we left off in the first part of this tutorial series. In this part of the tutorial, we first set up the mock parking spots data. And then we integrated them into the map screen using the map() array function.
Then, we also learned how to make the parking spot card in the parking section which can be scrolled horizontally. Lastly, we got the detailed guidance on implementing the smooth swiping/scrolling transition of parking spot card in the parking section.In the next part of this tutorial series, we are going to start implementing the parking spot card design as well as integrate additional data to them.So, Stay Tuned folks!!!