Within my React/Typescript project, I aim to dynamically exhibit a color based on the presence or absence of a value in payload[1]
. In the code snippet below, note the usage of an inline style
tag.
<li className="recharts-tooltip-item" style={{ payload[1] ? color : `${payload[1].color}` }}>
{payload[0] ? `${payload[1].dataKey} : ${payload[1].value}` : ""}
</li>
Despite referencing suggestions from A better way to change the background color using a ternary operator, I encountered an error: Type '{ payload: Payload<ValueType, NameType>[]; 1: any; }' is not assignable to type 'Properties<string | number, string & {}>'. This indicates that an object literal may solely specify known properties, and 'payload' isn't recognized within type 'Properties<string | number, string & {}>'
Code Update:
const CustomTooltip = ({ active, payload, label}: TooltipProps<ValueType, NameType>) => {
if(active && payload && payload.length){
return (
<div className = "custom-tooltip">
<ul className="recharts-tooltip-item-list" style={{padding: '0px', margin: '0px', listStyle:'none'}}>
<li className="recharts-tooltip-item" style={{ color: payload[0] ? color : `${payload[0].color}`}} >
{payload[0] ? `${payload[0].dataKey} : ${payload[0].value}` : ""}
</li>
</ul>
</div>
);
}
return null;
}