I'm building a rating component that takes in a number of icons and displays them using react-icons
. My question however is for a feature that I would like to implement. Currently, the react-icon that I'm using is FaStarhalf
which is a pre-filled half star, that I flip to make the illusion that I've created half stars. This is very easy to work with and does what I need it to do, however, I wanted to implement a better way to display when you are selecting a star. Unfortunately, react-icons
doesn't seem to have the ability for me to fill an icon all the way. FaRegStarHalf
is another component from react-icons
which is basically a cut out of a star, however, you can't fill it in.
So my question to you. Would it be possible inside my FaStarHalf
component, when it is clicked, to change every FaStarHalf
into it's filled counterpart from react-icons
? Can I change a component from inside itself to be a different component?
Rater
import React, { useState } from 'react'
import { FaStarHalf } from 'react-icons/fa'
import './styles/Rater.css'
const Rater = ({ iconCount }) => {
const [rating, setRating] = useState(null)
const [hover, setHover] = useState(null)
return (
<div>
{[...Array(iconCount), ...Array(iconCount)].map((icon, i) => {
const value = (i + 1) / 2
return (
<label>
<input
type='radio'
name='rating'
value={value}
onClick={() => {
console.log(`value => `, value)
return setRating(value)
}}
/>
<div className='star-container'>
<div>
<FaStarHalf
className={i % 2 ? 'star-left' : 'star'}
color={value <= (hover || rating) ? '#ffc107' : '#e4e5e9'}
onMouseEnter={() => setHover(value)}
onMouseLeave={() => setHover(null)}
/>
</div>
</div>
</label>
)
})}
</div>
)
}
export default Rater
FaHalfStar
FaRegHalfStar