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

121
Visualizações
best way to handle conditions in React

I have this line of code which I feel so bad about it.

{paymentType === "CASH" && status !== "ACCEPTED" && <OnOffButton type="cash" on={status === "CREATED"?false:true} />}
{paymentType === "CASH" && status === "ACCEPTED" && <OnOffButton onClick={()=> status === "CREATED" ? setActionBox({type: 'CREATED'}) : setActionBox({type: 'ACCEPTED'})} type="cash" on={status === "CREATED"?false:true} />}

as you can see the condition difference between first and second line is the status, and for the first option, I don't want to have a onClick . is there a better way to handle this?

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

0

Here is a slightly better refactor of your logic:

import React from 'react'

const ExampleComponent = () => {
  const paymentType = 'CARD';
  const status = 'ACCEPTED';

  if (paymentType !== 'CASH') {
    return null;
  }

  const isOn = status === 'CREATED';
  const isAccepted = status === 'ACCEPTED';

  return (
    <OnOffButton
      type="cash"
      on={isOn}
      onClick={() => isAccepted && setActionBox({ type: status })}
    />
  )
}

export default ExampleComponent
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