Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react native 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

react native rating

yarn add react-native-ratings

OR

npm install react-native-ratings --save
Comment

PREVIOUS NEXT
Code Example
Javascript :: Search products from an array by keywords in javascript 
Javascript :: how to use moment in angular 8 
Javascript :: use variable in form action vuejs 
Javascript :: javascript pdf 
Javascript :: nestjs forRoutes middlewarwe 
Javascript :: play 2 audio react 
Javascript :: NGX loading Interceptor 
Javascript :: how to get data from jsonplaceholder 
Javascript :: mongoose search by keywords 
Javascript :: javascript set contains 
Javascript :: Pausing setInterval when page/ browser is out of focus 
Javascript :: javascript onclick parameters 
Javascript :: get gravatar javascript 
Javascript :: helperbird 
Javascript :: jacascript loop array 
Javascript :: Answer the following questions by identifying what unit of measurement to be used. 2pts. Brainly 
Javascript :: Schalte das jQuery Migrate Script ab 
Javascript :: The app structure generator Express 
Javascript :: template.json replacing text in files 
Javascript :: how to get last index of array in javascript 
Javascript :: AsyncStorage getAllKeys seperately 
Javascript :: One component overlapping on other in react.js app 
Javascript :: JavaScript querySelector - Group selector 
Javascript :: how to add class to only one selected row then remove it after selecting it again 
Javascript :: axios with load more 
Javascript :: find a big length friend from array javascript finding longest string in array in javascript 
Javascript :: getelementsbyclassname angular 
Javascript :: typeorm caching queries limit 
Javascript :: socket cheatsheet 
Javascript :: SuiteScript https.post a pdf file 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =