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

176
Visualizações
Show different <Button> element depending on ternary

I am trying to show different HTML Elements depending on a ternary and I don't know the proper syntax to it.

{valuePro ?
                {
                <Button
                    className="proButton"
                    secondary
                    onClick={triggerFileSelectPopup}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}
                >
                    Change
                </Button>
                <Button
                    className="proButton"
                    onClick={onDownload}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}>
                    Confirm
                </Button>
                 } :
                {
                <GenericButton
                    secondary
                    onClick={triggerFileSelectPopup}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}
                >
                    Change
                </GenericButton>
                <GenericButton
                    onClick={onDownload}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}>
                    Confirm
                </GenericButton>
                }
}

I want to show when valuePro is true and when valuePro is false.

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

0

Wrapping multiple nested elements in a <> should make it work if that's the error you're caught up with.

{valuePro ?
                <>
                    <Button
                        className="proButton"
                        secondary
                        onClick={triggerFileSelectPopup}
                        style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}
                    >
                        Change
                    </Button>
                    <Button
                        className="proButton"
                        onClick={onDownload}
                        style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}>
                        Confirm
                    </Button>
                </> :
                <>
                    <GenericButton
                        secondary
                        onClick={triggerFileSelectPopup}
                        style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}
                    >
                        Change
                    </GenericButton>
                    <GenericButton
                        onClick={onDownload}
                        style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}>
                        Confirm
                    </GenericButton>
                </>
}
about 4 years ago · Juan Pablo Isaza Relatório

0

We can do that by actually picking up what component do we want to render:

const ButtonComponent = valuePro ? GenericButton : Button;

You also have common styles in there which u can extract for better readability here:

const calculatedStyles = { marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" };

Your code can look like this:

              <React.Fragment>
                <ButtonComponent 
                    secondary
                    onClick={triggerFileSelectPopup}
                    style={calculatedStyles}
                >
                    Change
                </ButtonComponent>
                <ButtonComponent 
                    onClick={onDownload}
                    style={calculatedStyles}>
                    Confirm
                </ButtonComponent>
              <React.Fragment>
}

Note that u can replace <React.Fragment></React.Fragment> with <></>

about 4 years ago · Juan Pablo Isaza Relatório

0

{valuePro ?
                <>
                <Button
                    className="proButton"
                    secondary
                    onClick={triggerFileSelectPopup}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}
                >
                    Change
                </Button>
                <Button
                    className="proButton"
                    onClick={onDownload}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}>
                    Confirm
                </Button>
                 </> :
                <>
                <GenericButton
                    secondary
                    onClick={triggerFileSelectPopup}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}
                >
                    Change
                </GenericButton>
                <GenericButton
                    onClick={onDownload}
                    style={{ marginRight: "10px", zIndex: (isVisible ? 6 : 1), marginBottom: "8px" }}>
                    Confirm
                </GenericButton>
                </>
}

try this since ternary if wants to get only 1 element

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