Logo

dev-resources.site

for different kinds of informations.

Building a Custom Star Rating Component in React Native with Sliding and Press Interactions

Published at
1/6/2025
Categories
reactnative
ratt
rating
app
Author
yousufdev702
Categories
4 categories in total
reactnative
open
ratt
open
rating
open
app
open
Author
12 person written this
yousufdev702
open
Building a Custom Star Rating Component in React Native with Sliding and Press Interactions

In mobile applications, interactive UI components like star rating systems significantly enhance user experience. In this blog post, we'll walk through building a custom star rating component in React Native that supports both sliding and press interactions for setting ratings. This component will also provide immediate visual feedback, enhancing the user interface and user engagement.

Prerequisites

Before we begin, ensure you have the following set up:

  • React Native development environment
  • Basic knowledge of React Native and TypeScript

Creating the Custom Star Rating Component

Let's break down the implementation of the CustomRatingStar component.

1. Setting Up the Component

We start by creating a functional component CustomRatingStar that accepts props like totalStars, initialRating, and onRate. The component maintains two state variables: rating for the final rating and hoverRating for the intermediate rating during sliding interactions.

import React, { useState, useRef } from "react";
import {
  StyleSheet,
  View,
  PanResponder,
  GestureResponderEvent,
  TouchableWithoutFeedback,
} from "react-native";
import { StartIcon } from "src/NativeBaseIcon";
import resSize from "@theme/resSize";

interface RatingProps {
  totalStars?: number;
  initialRating?: number;
  onRate?: (rating: number) => void;
}

const CustomRatingStar: React.FC<RatingProps> = ({
  totalStars = 5,
  initialRating = 0,
  onRate,
}) => {
  const [rating, setRating] = useState(initialRating);
  const [hoverRating, setHoverRating] = useState(initialRating);
  const containerRef = useRef<View | null>(null);
Enter fullscreen mode Exit fullscreen mode

2. Handling Sliding and Press Interactions

To handle sliding interactions, we use the PanResponder API. This API tracks gestures, allowing us to update the hoverRating as the user slides their finger across the stars. We also handle press interactions by calculating the rating based on the touch position relative to the container.

  const updateRating = (positionX: number, width: number, isFinal: boolean = false) => {
    const starWidth = width / totalStars;
    let newRating = Math.ceil(positionX / starWidth);
    newRating = Math.max(1, Math.min(newRating, totalStars));

    if (isFinal) {
      setRating(newRating);
      if (onRate) {
        onRate(newRating);
      }
    } else {
      setHoverRating(newRating);
    }
  };

  const panResponder = useRef(
    PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onMoveShouldSetPanResponder: () => true,
      onPanResponderMove: (_, gestureState) => {
        if (containerRef.current) {
          containerRef.current.measure((_, __, width, ___, pageX) => {
            const positionX = gestureState.moveX - pageX;
            updateRating(positionX, width);
          });
        }
      },
      onPanResponderRelease: (_, gestureState) => {
        if (containerRef.current) {
          containerRef.current.measure((_, __, width, ___, pageX) => {
            const positionX = gestureState.moveX - pageX;
            updateRating(positionX, width, true);
          });
        }
      },
    })
  ).current;

  const handlePress = (event: GestureResponderEvent) => {
    event.persist();
    if (containerRef.current) {
      containerRef.current.measure((_, __, width, ___, pageX) => {
        if (event.nativeEvent && width) {
          const positionX = event.nativeEvent.pageX - pageX;
          updateRating(positionX, width, true);
        }
      });
    }
  };
Enter fullscreen mode Exit fullscreen mode

3. Rendering the Stars

We use a loop to render the stars, and the color of each star is determined by comparing the current index with the hoverRating.

  return (
    <TouchableWithoutFeedback onPress={handlePress}>
      <View>
        <View
          ref={containerRef}
          style={styles.container}
          {...panResponder.panHandlers}
        >
          {Array.from({ length: totalStars }, (_, index) => (
            <StartIcon
              key={index}
              size={resSize(35)}
              color={index < hoverRating ? "#FF9900" : "#FFFFFF"}
            />
          ))}
        </View>
      </View>
    </TouchableWithoutFeedback>
  );
};
Enter fullscreen mode Exit fullscreen mode

4. Adding Styles

Finally, we define some basic styles for the component.

const styles = StyleSheet.create({
  container: {
    flexDirection: "row",
    justifyContent: "center",
    alignItems: "center",
    gap: 4,
  },
  ratingText: {
    marginTop: 10,
    textAlign: "center",
    fontSize: resSize(18),
    color: "#FF9900",
  },
});

export default CustomRatingStar;
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we built a custom star rating component in React Native that supports both sliding and press interactions. By utilizing React Native's PanResponder and state management, we provided a smooth and responsive user experience. This component can be a valuable addition to any mobile application requiring user feedback through a rating system.

Feel free to customize this component further to fit your application's needs. Happy coding!

reactnative Article's
30 articles in total
Favicon
Using Direct Line botframework in a React Native Application to connect to Copilot Studio Agent
Favicon
[Video]Build a Full-Stack Mobile App with React Native Expo and Payload CMS in 2025!
Favicon
Introducing EAS Hosting: Simplified deployment for modern React apps
Favicon
Auto Resize multiline TextInput in React Native
Favicon
Read Text Asset File in Expo
Favicon
Flat list horizontal all Items perfectly visible in iOS not in android ContentContainerStyle
Favicon
Building High-Performance React Native Apps[Tips for Developers]
Favicon
React Native With TypeScript: Everything You Need To Know
Favicon
Building the 'One of a Kind' Ultimate Mobile App Framework. Seeking exceptional engineers to join the journey.
Favicon
Ship mobile apps faster with React-Native-Blossom-UI
Favicon
Encryption in React Native apps enhances data security, protecting user information and ensuring privacy. However, it also presents challenges, such as performance overhead and complex implementation
Favicon
How to Integrate Stack, Bottom Tab, and Drawer Navigator in React Native
Favicon
🌎 Seamless Multi-Language Support in React Native
Favicon
Mastering React Native with TypeScript: From Basics to Brilliance - Part 1
Favicon
Building "Where Am I?": A GeoGuessr Alternative for Mobile
Favicon
React Native is powerful modern technology
Favicon
How to Build a Centered Input Field for Amounts in React Native
Favicon
Compound Component pattern in react
Favicon
Pop Quiz: Is There a Bug in This React Native Component?
Favicon
The Unlikely Fix: How a Simple Folder Change Saved My React Native Journey
Favicon
How to Integrate Stack and Bottom Tab Navigator in React Native
Favicon
How to make a view expand downwards in an inverted FlatList?
Favicon
Building a Custom Star Rating Component in React Native with Sliding and Press Interactions
Favicon
Choosing the Right Compiler for React Native Development in 2025
Favicon
Simple remove transition animation in react native
Favicon
How to Hire Dedicated React Native Developers for Customizable Features
Favicon
React Native’s New Architecture: Sync and async rendering
Favicon
Top Mobile App Development Company in Bangalore | Hyena IT
Favicon
Mastering Import Order in React: A Deep Dive Into Best Practices and Tools
Favicon
Creating Custom Inputs for Regex Validation in React and React Native

Featured ones: