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


Without seeing what the data you're working with looks like this is difficult to answer but I'll take a shot. I'm assuming that what you want is when a user clicks a star to set a rating between 1 and 5, you want to show their rating to them in the form of filled stars. If that's the case you can use
ratingto determine whether the user is being shown the current rating or the rating that they've set and then use the proper icon as needed.This can be accomplished by setting the proper string to a variable and then using that in place of your icons JSX tag name. Basically, if the user has set a rating,
ratingwill no longer benulland you can assert that you need filled stars. Use that logic to assign a variable to the string you want.const Star = rating ? 'FaStar' : 'FaHalfStar'