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

99
Views
Conditional rendering of multiple elements

I want ro render 3 additional buttons if the current user is the user that created the group i'm in at the moment, so i tried someting like this :

render(){
        let adminButtons;
        if (this.state.groupData && this.props.user.id){
            if(this.state.groupData.owner === this.props.user.id){
                adminButtons = {
                    <CustomButton>Add Quiz</CustomButton>
                    <CustomButton>Drafts</CustomButton>
                    <CustomButton>Delete Group</CustomButton> };
            }
            else{
                adminButtons = <div/>;
            }
            }
        }
    return(
        <div className='group-page'>
            <div className='group-controls'>

                <div className='admin-buttons'>
                    {adminButtons}
                </div>   
            </div>

        </div>
    )}

But this implementation raises a compiler error , how could i change up the code in order to make it work? JSX experssion must have one parent element , is the error i'm getting

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

0

The short answer is you can wrap your components in a React.Fragment.

 <>
  <CustomButton>Add Quiz</CustomButton>
  <CustomButton>Drafts</CustomButton>
  <CustomButton>Delete Group</CustomButton>
</>

As per the docs:

A common pattern in React is for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM.

about 4 years ago · Juan Pablo Isaza Report

0

here you need minor change

adminButtons = {
                    <CustomButton>Add Quiz</CustomButton>
                    <CustomButton>Drafts</CustomButton>
                    <CustomButton>Delete Group</CustomButton> };
            }

to

adminButtons = [
                    <CustomButton>Add Quiz</CustomButton>
                    <CustomButton>Drafts</CustomButton>
                    <CustomButton>Delete Group</CustomButton> };
            ]

Ideally JSX need either string, null, html/jsx elements or array of either of these. you are using object, that's why you got compile errors.

about 4 years ago · Juan Pablo Isaza Report

0

render() {
  let adminButtons;
  if (this.state.groupData && this.props.user.id){
    if (this.state.groupData.owner === this.props.user.id){
      adminButtons = [
        <CustomButton>Add Quiz</CustomButton>,
        <CustomButton>Drafts</CustomButton>,
        <CustomButton>Delete Group</CustomButton> ];
    } else {
      adminButtons = <div/>;
    }
  }

  return(
    <div className='group-page'>
      <div className='group-controls'>
        <div className='admin-buttons'>
          {adminButtons}
        </div>   
      </div>      
  </div>
)}
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!