REACT create half star rating react - Net-Raft.com

Welcome aboard! We are happy you are here and wish you good net-raft!




Just a Web Code Solution
join us

create half star rating in react


 1079

Show ResultREACT


2

/* based on https://codesandbox.io/s/xpl2wjpyoq */
<html lang = "en">

<head>
<meta charset = "UTF-8">
<title>React App</title>
<script src="https://fb.me/react-0.14.3.min.js"></script>
<script src="https://fb.me/react-dom-0.14.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

</head>

<body>

<script type="text/babel">

class Rating extends React.Component {
constructor(props) {
super(props);
this.state = {
rating: this.props.rating || null,
temp_rating: null
};
}

handleMouseover(rating) {
this.setState(prev => ({
rating,
temp_rating: prev.rating
}));
}

handleMouseout() {
this.setState(prev => ({
rating: prev.temp_rating
}));
}

rate(rating) {
this.setState({
rating,
temp_rating: rating
});
}

render() {
const { rating } = this.state;
let stars = [];
for (let i = 0; i < 10; i++) {
console.log("i", i);
let klass = "ion-ios-star-outline";
if (this.state.rating >= i && this.state.rating !== null) {
klass = "ion-ios-star";
}
stars.push(
<i
style={{ display: "inline-block", width: "7px", overflow: "hidden", direction: (i%2===0) ?
"ltr" : "rtl"}}
className={klass}
onMouseOver={() => this.handleMouseover(i)}
onClick={() => this.rate(i)}
onMouseOut={() => this.handleMouseout()}
/>
);
}

return (
<div className="rating">
{stars}
</div>
);
}
}

const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};

ReactDOM.render(
<div style={styles}>
<Rating rating={5} />
</div>,
document.getElementById('root')
);

</script>

<div id="root"></div>

</body>

</html>


By        
The power of the user (%)
3%



The most helpful REACT solutions

How to get client ip address using react ?How to get client ip address using react ?REACT

Click to see more ...

  19.5K     421

how to get the type of a device using react ?how to get the type of a device using react ?REACT

Click to see more ...

  2.3K     220

create digital clock reactcreate digital clock reactREACT

Click to see more ...

  1.7K     141

country dropdown using reactcountry dropdown using reactREACT

Click to see more ...

  1.3K     127

create alarm clock using reactcreate alarm clock using reactREACT

Click to see more ...

  5.3K     115

How to get latitude and longitude using react/javascript ?How to get latitude and longitude using react/javascript ?REACT

Click to see more ...

  1.1K     108

get client country reactget client country reactREACT

Click to see more ...

  885     82

get ip address reactget ip address reactREACT

Click to see more ...

  1.1K     81

time ago reacttime ago reactREACT

Click to see more ...

  696     74

detect mobile device reactdetect mobile device reactREACT

Click to see more ...

  658     58

Welcome aboard!
We are happy you are here and
wish you good net-raft!