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

86
Views
REACT- Fill checkbox if value=1 else not filled

how can I create a checkbox that is fill when item.availability=1 (for example) elif 0 it's not filled. I can return {item.availability} in Checkbox function but I want something more generique.

export default function Display() {
  ...      
  function Checkbox({ value }) {
    const [checked, setChecked] = React.useState(true);

    return (
      <label>
        <input
          type="checkbox"
          defaultChecked={checked}
          onChange={() => setChecked(!checked)}
        />
        {value}
      </label>
    );
  }
  return (
    <>
          ....
          {"Availability "}
          <Checkbox ({item[0].availability})/>
        </div>
      </div>
    </>
  );
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can just set the received value prop as initial state of the checked state. Then make the checked prop in the input element depending on the state:

function Checkbox({ value }) {
    const [checked, setChecked] = React.useState(value);

    return (
      <label>
        <input
          type="checkbox"
          checked={checked}
          onChange={() => setChecked(checked => !checked)}
        />
        {value}
      </label>
    );
  }

Make sure to pass the prop correctly:

<Checkbox value={!!item.availability} />
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!