Build Real-World React Native App #4 : Content Placeholder

Build Real-World React Native App #4 : Content Placeholder

Build Real-World React Native App #4 : Content Placeholder

Placeholders are an essential addition to the overall UI of any app. They enable us to show the temporary image or text until the actual data loads. Using them, users can know that the content is yet to load in the section placeholders are shown. With there addition, users can even gauge out the internet connectivity. The longer the placeholder stands, the slower the connectivity. Hence, the content placeholders provide a visual impact in the overall UI. Thus, optimizing the user experience to another level.

In this chapter, we will create a preloader using the component from the rn-placeholder package and a preloader for the image using the component from the image-placeholder package. For content, we are using rn-placeholder that displays some placeholder stuff before rendering your text or media content in React Native. For the image, we are using the image-placeholder that supports both Android and iOS. It also provides Activity Indicator customization. The preloaders help to provide a better user experience by showing the loading effect until the actual data loads. This will help users know that the data to be shown on the screen is loading. Its slowness depicts the disturbance in internet connection. Hence, it is a very useful feature for a great user experience.

The idea is to set up the Pre-loader component for content in a separate component file and render it out conditionally in the post list. For the image placeholder, we are going to configure it and then add it to the FlatList component.

Let’s get started!

Creating a preloader Component

First, we need to install rn-placeholder using yarn in the project terminal:

For preloader style, we are going to use a style similar to the Facebook app which we see every day. The example preloader style is shown below:

Preloader example

Preloader example

Now, we need to create component name ContentPlaceholder.js as shown in the screenshot below:

Component structure

Component structure

In the file, we need to import all the necessary components from rn-placeholder package as shown in the code snippet below:

Here, we are creating a loop inside a function called PlaceHolderObject. Then, we need to add the placeholder to view by calling the function as shown in the code snippet below:

Hence, we have the placeholder component to be used in other components to show the preloaders.

Now, we need to learn how to use them in different components.

How to use

First, we need to import it to the screen file that we want to set preload such as Home and SinglePost. For that, we need to use the code from the following code snippet:

Then, we need to create a state called isLoading in order to handle the loading state as shown in the code snippet below:

Here, while the initial state is true we need to display the preloader until we get response data from the server. For that, we need to use the code from the following code snippet:

Here, the function fetchLatestPost is configured to fetch the date from the server. And, the loading is set based on the data being fetched.

Now, we need to hide/show the preloaders based on the state value. For that, we need to use the code from the following code snippet:

Hence, we will get the result as shown in the emulator screenshots below:

React Native placeholder

React Native placeholder

Image placeholder

The package that we used for the content placeholder above does not support images. Thus, we need to use a different package for image preloaders. Hence, the package we are going to use is react-native-image-placeholder.

First, we need to install the package using the following command:

Then, we need to import it to screen files that have images in it such as Home, SinglePost, etc as shown in the code snippet below:

Next, we need to replace <Image> component with ImageLoad component and set the attributes in order to adjust the loading icon as shown in the code snippet below:

Now, if we re-run the app, we will get the following result on the emulator screen:

React Native image placeholder

React Native image placeholder

Here, we can see the spinner-like icon at the center of the image area which is an image preloader.

Extracting Flatlist

Until now, we have many duplicate FlatList components on multiple screens. In order to optimize it, we need to create a separate FlatlistItem component. For that, we need to create a new file called FlatlistItem.js first. Then, we can use the code from the following code snippet into this file for creating a FlatList component that can be used in multiple screens:

Now, in order to use the FlatlistItem component in Home Screen, we need to import the FlatlistItem component to Home screen as shown in the code snippet below:

Next, we need to replace the old Flatlist component as highlighted in the code snippet below:

Note that, we must not forget to pass the navigation object in the Home functional component as shown in the code snippet below:

Finally, we have successfully created the content placeholder component, integrated the image preloaders, and also configured the separate FlatList component which can be used on multiple screens.

Conclusion

The overall tutorial was simple and the implementation was also easy. This will help further optimize the user experience based on the React Native app. Content placeholders are an important concept to learn as it is used everywhere web, mobile, and desktop apps as well.

In this chapter, we got an insight into improving user experience by integrating content and image placeholders. We got stepwise guidance on how to integrate them to different screens with server data to be loader. Moreover, we also learned how to optimize the code by extracting Flatlist items to a single component. Hence, using the Flatlist component in every screen that requires FlatList.

All code in this chapter is available on GitHub.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

Build Real-World React Native App #9 : Implementing Remove Ads Feature

One of the simplest ways to make money from apps is through Advertisements. Hence, we have a popular ads network for mobile that is Admob. There are ...

Build Real-World React Native App #8 : implement Dark mode

In this chapter, we are going to implement a new feature in our app. The feature is called dark mode which is very prevalent in the app nowadays. The ...

Build Real-World React Native App #6: Show Bookmark and Categories

Here, we are going to implement the list view of bookmarked posts in the Bookmark screen and also work on implementing the Categories screen. For the ...

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy