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

226
Views
ReactJS Conditional Rendering - else if doesn't work

I'm in the stage of learning JS and React and I'm not sure why else if doesn't work in the code below. Anyone can help me?

  function Item({ name, isPacked }) {
  if (isPacked) {
    return (
      <li className="item">
        {name} {isPacked && " ✔"}
      </li>
    );
  } else if (!isPacked) {
    return (
      <li className="item">
        {name} {isPacked && " ❌"}
      </li>
    );
  }
}

export default function PackingList() {
  return (
    <section>
      <h1>Sally Ride's Packing List</h1>
      <ul>
        <Item isPacked={true} name="Space suit" />
        <Item isPacked={true} name="Helmet with a golden leaf" />
        <Item isPacked={false} name="Photo of Tam" />
      </ul>
    </section>
  );
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try like this:

function Item({ name, isPacked }) {
    return (
        <li className="item">
            {`${name} ${isPacked ? "✔" : '❌'} `}
        </li>
    );
}
about 4 years ago · Juan Pablo Isaza Report

0

you actually tested if the isPacked true or false so try this code:

function Item({ name, isPacked }) {
    return (
      <li className="item">
        {name}  {isPacked ? "✔" : "❌" }
      </li>
    );
}

{isPacked ? "✔" : "❌" } so this line of code is equal to:

  if(isPacked == true){
      return "✔";
    }else{
      return "❌";
    }

You can get some helpful examples Here https://reactjs.org/docs/conditional-rendering.html.

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!