Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Mostrar diferentes elementos <Button> dependiendo del ternario

Estoy tratando de mostrar diferentes elementos HTML según un ternario y no conozco la sintaxis adecuada.

 {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> } }

Quiero mostrar cuándo valuePro es verdadero y cuándo valuePro es falso.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Envolver múltiples elementos anidados en un <> debería hacer que funcione si ese es el error con el que te encuentras.

 {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 Report

0

Podemos hacer eso eligiendo qué componente queremos renderizar:

 const ButtonComponent = valuePro ? GenericButton : Button;

También tiene estilos comunes allí que puede extraer para una mejor legibilidad aquí:

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

Su código puede verse así:

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

Tenga en cuenta que puede reemplazar <React.Fragment></React.Fragment> con <></>

about 4 years ago · Juan Pablo Isaza Report

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> </> }

intente esto desde ternario si quiere obtener solo 1 elemento

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!