Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react native product rating

// React Native Custom Star Rating Bar
// https://aboutreact.com/react-native-custom-star-rating-bar/

// import React in our code
import React, { useState } from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  StyleSheet,
  View,
  Text,
  Image,
  TouchableOpacity,
} from 'react-native';

const App = () => {
  // To set the default Star Selected
  const [defaultRating, setDefaultRating] = useState(2);
  // To set the max number of Stars
  const [maxRating, setMaxRating] = useState([1, 2, 3, 4, 5]);

  // Filled Star. You can also give the path from local
  const starImageFilled =
    'https://raw.githubusercontent.com/AboutReact/sampleresource/master/star_filled.png';
  // Empty Star. You can also give the path from local
  const starImageCorner =
    'https://raw.githubusercontent.com/AboutReact/sampleresource/master/star_corner.png';

  const CustomRatingBar = () => {
    return (
      <View style={styles.customRatingBarStyle}>
        {maxRating.map((item, key) => {
          return (
            <TouchableOpacity
              activeOpacity={0.7}
              key={item}
              onPress={() => setDefaultRating(item)}>
              <Image
                style={styles.starImageStyle}
                source={
                  item <= defaultRating
                    ? { uri: starImageFilled }
                    : { uri: starImageCorner }
                }
              />
            </TouchableOpacity>
          );
        })}
      </View>
    );
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        {/*View to hold our Stars*/}
        <CustomRatingBar />
        <Text style={styles.textStyle}>
          {/*To show the rating selected*/}
          {defaultRating} / {Math.max.apply(null, maxRating)}
        </Text>
        <TouchableOpacity
          activeOpacity={0.7}
          style={styles.buttonStyle}
          onPress={() => alert(defaultRating)}>
          {/*Clicking on button will show the rating as an alert*/}
          <Text style={styles.buttonTextStyle}>Get Selected Value</Text>
        </TouchableOpacity>
      </View>
    </SafeAreaView>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
    justifyContent: 'center',
    textAlign: 'center',
  },
  titleText: {
    padding: 8,
    fontSize: 16,
    textAlign: 'center',
    fontWeight: 'bold',
  },
  textStyle: {
    textAlign: 'center',
    fontSize: 23,
    color: '#000',
    marginTop: 15,
  },
  textStyleSmall: {
    textAlign: 'center',
    fontSize: 16,
    color: '#000',
    marginTop: 15,
  },
  buttonStyle: {
    justifyContent: 'center',
    flexDirection: 'row',
    marginTop: 30,
    padding: 15,
    backgroundColor: '#8ad24e',
  },
  buttonTextStyle: {
    color: '#fff',
    textAlign: 'center',
  },
  customRatingBarStyle: {
    justifyContent: 'center',
    flexDirection: 'row',
    marginTop: 30,
  },
  starImageStyle: {
    width: 40,
    height: 40,
    resizeMode: 'cover',
  },
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: create object javascript dynamically 
Javascript :: javascript get string from array with space between 
Javascript :: map array method create object 
Javascript :: how to disable and enable a button in jquery 
Javascript :: promise.race polyfill 
Javascript :: node get package.json version 
Javascript :: sequelize migration 
Javascript :: nanoid 
Javascript :: discord bot javascript 
Javascript :: why does my form reload the page? html js 
Javascript :: javascript math methods 
Javascript :: how to loop through something in node.js 
Javascript :: codeblocks in html cdnjs 
Javascript :: js create p element with text 
Javascript :: jquery show hide based on data attribute 
Javascript :: React Native Starting In Android 
Javascript :: javascript emit event 
Javascript :: how to find duplicate values in an array javascript 
Javascript :: js retour à la ligne 
Javascript :: how to check checkbox using jquery 
Javascript :: js check if a variable is an array 
Javascript :: array.from js 
Javascript :: react hooks componentdidmount 
Javascript :: check array values equal js 
Javascript :: remove duplicates in an array in javascript 
Javascript :: round innerhtml up javascript 
Javascript :: angular get device information 
Javascript :: randint js 
Javascript :: export data in json format in javascript 
Javascript :: how to show selected value in select box using jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =