I want to show the battery icon given the status
provided in props. I am trying to keep it simple but without writing too much code, i.e.: . if ... else
, switch
s statements...
Below code snippet works but it kinda looks forced, not sure.
import {
TiBatteryLow,
TiBatteryMid,
TiBatteryHigh,
TiBatteryFull
} from 'react-icons/ti'
const battery = {
low: {
color: "text-red-500",
icon: TiBatteryLow
},
mid: {
color: "text-yellow-400",
icon: TiBatteryMid
},
high: {
color: "text-green-400",
icon: TiBatteryHigh
},
full: {
color: "text-green-600",
icon: TiBatteryFull
}
};
export default function BalanceIndicator({status}) {
let Balance = battery[status].icon;
return (
<div className="text-sm flex items-center">
<i className={`mr-3 text-2xl ${battery[status].color}`}><Balance /></i>
</div>
)
}
Is there a better way to do this? Perhaps conditional rendering?
PS: In title, by condition I mean the value of status
.