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

191
Visualizações
How to render a different element based on a condition (react jsx)

I want to show a certain button inside a data table in the UI based on a condition

pseudocode

if assessment.category is complete
//show success button

if assessment.category is not complete
//show failed button

if assessment.category is in progress
//show in progress button

if assessment.category is rejected
//show rejected button

(JSX)

<td>
  {assessment.categoryCategory === 'Complete' && <button type="button" className="btn btn-success">Complete</button> 
  }
  {assessment.categoryCategory === 'Not Complete' && <button type="button" className="btn btn-info">Not Completed</button>
  }
  {assessment.categoryCategory === 'In Progress' && <button type="button" className="btn btn-warning">WIP</button>
  }
  {assessment.categoryCategory === 'Rejected' && <button type="button" className="btn btn-danger">Rejected</button>
  }
</td>

However this is wrong. It throws an error. Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const btnMap = {
 complete : { key: 'warning', label: 'Completed' },
 'not complete' : { key: 'info', label: 'Not Completed' },
 progress : { key: 'warning', label: 'WIP'},
 rejected : { key: 'danger', label: 'Rejected' },
 // easy to add additional values if needed.
}
const btnItem = btnMap[assestment.category || 'not progress']; //set a default value if you believe could be undefined.
 
 <button 
   type="button" 
   className={`btn btn-${btnItem.key}`}>
    {btnItem.label}
 </button>
about 4 years ago · Juan Pablo Isaza Relatório

0

<td>
  {
   assessment.categoryCategory === 'Complete' ?  (
    <button type="button" className="btn btn-success">Complete</button>
    ) : (
    <button type="button" className="btn btn-info">Not Completed</button>
    )
  }
</td>
about 4 years ago · Juan Pablo Isaza Relatório

0

Why not simplify it like this?

<td>
  <button type="button" className={assessment.categoryCategory === 'Complete' ? "btn btn-success" : "btn btn-info"}>
    {assessment.categoryCategory === 'Complete' ? "Complete" : "Not Complete"}
  </button>
</td>
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