I've created a Slider with labels in react. When the user moves the Slider the numbers underneath get highlighted, I achieved it by giving the number class like this.
<span
className={scaleHighlightClass[index]}
key={index}
ref={scaleElement}
onClick={() => handleClickOnScale(index)}
>
The scaleHighlightClass array uses a Hook like so:
const [scaleHighlightClass, setScaleHighlightClass] = useState<string[]>(["selected"])
When the user moves the slider (which is a input with type=range) this function gets triggerd.
const highlightSelected = (selectedNumber) => {
// Reset all others
for (var i = 0; i < scaleHighlightClass.length; i++) {
scaleHighlightClass[i] = ""
}
// Set the clicked/slided to as selected
setScaleHighlightClass([...scaleHighlightClass])
}
It does work as expected but when using the Slider it triggers a re-render for every number it passes by. It is intended since I want to have this effect, but the question is, is this effective in terms of performance?
Is there a better way to change classes dynamically.