Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Ternary Operator Inline style tag React

In my React/Typescript application, I want to conditionally display a color depending if the value of payload[1] is null or not. As seen below, I have the style tag inline.

<li className="recharts-tooltip-item" style={{ payload[1] ? color : `${payload[1].color}` }}>
    {payload[0] ? `${payload[1].dataKey} : ${payload[1].value}` : ""}
</li>

I have tried following suggestions from, A better way to change the background color using a ternary operator, but I am getting error: Type '{ payload: Payload<ValueType, NameType>[]; 1: any; }' is not assignable to type 'Properties<string | number, string & {}>'. Object literal may only specify known properties, and 'payload' does not exist in type 'Properties<string | number, string & {}>'

Update of Code:

  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;
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You haven't defined a property in the style prop properly. You're using the raw color value in the falsy situation. I'm pretty sure this is what you're looking for

<li style={{
  color: payload[1] ? color : `${payload[1].color}`
}}>
  {payload[0] ? `${payload[1].dataKey} : ${payload[1].value}` : ""}
</li >

For your second problem, Make your own type definition for your component. Something along the lines of this should work

type CustomTooltipProps = Omit<TooltipProps<ValueType, NameType>, 'payload'> & {
  payload: {
    dataKey: any;
    value: any;
    color: any;
  }[]
}
about 4 years ago · Juan Pablo Isaza Relatório

0

<li className="recharts-tooltip-item"style={{ color: payload[1] ? "red" : `${payload[1].color}` }}>

Can you try to write the code like this?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda