Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda