Create Simple Dynamic React Native Rating Bar Android iOS Tutorial

Rating bar is very common and most widely used GUI in web and mobile technologies where we want user experience about specific product / term / item. We can create simple rating bar in react native with simple piece of code or without the help of any plugin / library. Through this tutorial, I am going to explain and show you how we can create simple rating bar in react native with complete source from scratch.

Note: In this tutorial, I am using two nice star_border and star_fill icon images. You can download these star_border and star_fill icon images and create a folder( if not exist ) named assets under your react native project directory to access these icon images in your app.

Let’s get started…

Step – 1: Import required components from react , react-native  packages and define constant for max rating.

Step – 2: Implement constructor  method.

Step – 3: Implement changeRating  custom method which will be called every time when user will want to change the rating.

Explanation:

  • changeRating  custom method takes key as parameter which should lies between 1 to MAX_RATING constant, when we click on any star icon then this changeRating  function will be executed with its corresponding key value( started from 1 ) and then we will update the rating state variable with this key value.

Step – 4: Implement render method.

Explanation:

  • As you can see in the previous step, we are updating the rating state variable and as you know then we update the state then render method is called. So, exactly in our case when user will select any star icon then we will update the rating state variable and the updating in state will call the render method again with updated rating.
  • And as you can see in render method we are comparing the value of i with rating state variable. If user wants to give 3 rating then rating state variable will be updated with value 3 and according to condition if i <= this.state.rating  then we will use the star_fill image otherwise use star_border image.

Step – 5: Implement custom styles for all required components.

Complete Source Code:

Step – 6: Execute react-native run-ios  to run app in iOS devices and / or execute react-native run-android  to run app in Android devices.

  • iOS Screenshot:

  • Android Screenshot:

Enjoy Guys… 😀

Leave a Reply

Your email address will not be published. Required fields are marked *