The requirement is to pass in the rating data for the product, and the page will display the corresponding number of stars. 1. Prepare three star pictures corresponding to different ratings 2. Expected results Such Call <StarScore score={data.wm_poi_score}/> 3. Process the incoming data We need to get the integer and decimal parts of the score let wm_poi_score = this.props.score || ''; let score = wm_poi_score.toString(); let scoreArray = score.split('.'); If 4.7 is passed in, the resulting scoreArray is 4. Calculate the corresponding number of stars based on the data // Full star number let fullstar = parseInt(scoreArray[0]); // Number of half stars let halfstar = parseInt(scoreArray[1]) >= 5 ? 1 : 0; // Number of gray stars let nullstar = 5 - fullstar - halfstar; 5. Render components according to the number of stars let starjsx = []; // Render full stars for (let i = 0; i < fullstar; i++) { starjsx.push(<div key={i + 'full'} className="star fullstar"></div>) } // Render half star if (halfstar) { for (let j = 0; j < halfstar; j++) { starjsx.push(<div key={j + 'half'} className="star halfstar"></div>) } } // Render gray star if (nullstar) { for (let k = 0; k < nullstar; k++) { starjsx.push(<div key={k + 'null'} className="star nullstar"></div>) } } OK, the effect we want is achieved. 6. Manual Scoring When the page is first displayed, 5 gray stars are rendered. Add a click event to each star. When clicked, get the subscript index corresponding to the star and add a highlight style to it. <div className="star-area"> {this.renderStar()} </div> doEva(i) { this.setState({ startIndex: i + 1 }); } renderStar() { let array = [] for (let i = 0; i < 5; i++) { let cls = i >= this.state.startIndex ? "star-item" : "star-item light" array.push( <div onClick={() => this.doEva(i)} key={i} className={cls}></div> ) } return array } Complete code import React from 'react'; import './StarScore.scss'; // Render 5 star score method class StarScore extends React.Component { renderScore() { let wm_poi_score = this.props.score || ''; let score = wm_poi_score.toString(); let scoreArray = score.split('.'); // Full star number let fullstar = parseInt(scoreArray[0]); // Number of half stars let halfstar = parseInt(scoreArray[1]) >= 5 ? 1 : 0; // Number of gray stars let nullstar = 5 - fullstar - halfstar; let starjsx = []; // Render full stars for (let i = 0; i < fullstar; i++) { starjsx.push(<div key={i + 'full'} className="star fullstar"></div>) } // Render half star if (halfstar) { for (let j = 0; j < halfstar; j++) { starjsx.push(<div key={j + 'half'} className="star halfstar"></div>) } } // Render gray star if (nullstar) { for (let k = 0; k < nullstar; k++) { starjsx.push(<div key={k + 'null'} className="star nullstar"></div>) } } return starjsx; } render() { return <div className="star-score">{this.renderScore()}</div>; } } export default StarScore; StarScore.scss .star-score { .star { width: 10px; height: 10px; float: left; background-size: cover; } .fullstar { background-image: url('./img/fullstar.png'); } .halfstar { background-image: url('./img/halfstar.png'); } .nullstar { background-image: url('./img/gray-star.png'); } } import './Main.scss'; import React from 'react'; class Main extends React.Component { constructor(props) { super(props); } } /** * Click to rate */ doEva(i) { this.setState({ startIndex: i + 1 }); } /** * Stars for rendering rating */ renderStar() { let array = [] for (let i = 0; i < 5; i++) { let cls = i >= this.state.startIndex ? "star-item" : "star-item light" array.push( <div onClick={() => this.doEva(i)} key={i} className={cls}></div> ) } return array } render() { return ( <div className="content"> <div className="star-area"> {this.renderStar()} </div> </div> ); } } export default Main; .content { height: 100%; .eva-content { background-color: #fff; overflow: hidden; margin: px2rem(10px); margin-top: px2rem(74px); } .star-area { text-align: center; margin-top: px2rem(30px); } .star-item { width: px2rem(32px); height: px2rem(32px); background-image: url('./img/gray-star.png'); background-size: cover; display: inline-block; margin-right: px2rem(10px); &.light { background-image: url('./img/light-star.png'); } } } This is the end of this article about the implementation of the React star rating component. For more relevant React star rating content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of how to use CMD command to operate MySql database
Overview For small and medium-sized projects, joi...
Preface The this pointer in JS has always been a ...
Tab selection cards are used very frequently on r...
Table of contents 1. Custom routing 2. Tab naviga...
A record of an online MySQL transaction problem L...
With the increasing number of open platforms, the ...
background CVE-2021-21972 An unauthenticated comm...
Generally speaking, we can have the following two...
I use the simultaneous interpretation voice recog...
Preface This article records a problem I encounte...
1. Set up HOST on the host Macbook The previous d...
1. Check the kali linux system version Command: c...
Compared with other large databases such as Oracl...
text-shadow Add a shadow to the text. You can add...
Preface echarts is my most commonly used charting...