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

182
Views
How to render a different element based on a condition (react jsx)

I want to show a certain button inside a data table in the UI based on a condition

pseudocode

if assessment.category is complete
//show success button

if assessment.category is not complete
//show failed button

if assessment.category is in progress
//show in progress button

if assessment.category is rejected
//show rejected button

(JSX)

<td>
  {assessment.categoryCategory === 'Complete' && <button type="button" className="btn btn-success">Complete</button> 
  }
  {assessment.categoryCategory === 'Not Complete' && <button type="button" className="btn btn-info">Not Completed</button>
  }
  {assessment.categoryCategory === 'In Progress' && <button type="button" className="btn btn-warning">WIP</button>
  }
  {assessment.categoryCategory === 'Rejected' && <button type="button" className="btn btn-danger">Rejected</button>
  }
</td>

However this is wrong. It throws an error. Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.

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

0

const btnMap = {
 complete : { key: 'warning', label: 'Completed' },
 'not complete' : { key: 'info', label: 'Not Completed' },
 progress : { key: 'warning', label: 'WIP'},
 rejected : { key: 'danger', label: 'Rejected' },
 // easy to add additional values if needed.
}
const btnItem = btnMap[assestment.category || 'not progress']; //set a default value if you believe could be undefined.
 
 <button 
   type="button" 
   className={`btn btn-${btnItem.key}`}>
    {btnItem.label}
 </button>
about 4 years ago · Juan Pablo Isaza Report

0

<td>
  {
   assessment.categoryCategory === 'Complete' ?  (
    <button type="button" className="btn btn-success">Complete</button>
    ) : (
    <button type="button" className="btn btn-info">Not Completed</button>
    )
  }
</td>
about 4 years ago · Juan Pablo Isaza Report

0

Why not simplify it like this?

<td>
  <button type="button" className={assessment.categoryCategory === 'Complete' ? "btn btn-success" : "btn btn-info"}>
    {assessment.categoryCategory === 'Complete' ? "Complete" : "Not Complete"}
  </button>
</td>
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!