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

160
Views
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 answers
Answer question

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 Report

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

try this since ternary if wants to get only 1 element

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!