Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda