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

104
Visualizações
Not able to update the checkbox using onChange

I am not able to Change the value of checkbox from false to true onclick on checkbox the onchange is not getting called.

checkedbox should be changed to true whwn ever we clicked the checkbox

this.state = {
  checkedbox: false,
}

 render() {
 <Checkbox
   label="test"
   value={checkedbox}
   onChange={this.handelCheck}
  />
}



 handelCheck = (event) => {
this.setState({
  checkedbox: event.target.checked
});
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to use this.state in value, try the following

value={this.state.checkedbox}
about 4 years ago · Juan Pablo Isaza Relatório

0

Simple, try using checked attribute for checkbox

Example

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. It depends how you're logging that state after you've changed it. setState provides a callback that you can use to do things after the async process has completed, like logging the new state.

  2. Depending on whether you're using a library or rolling your own Checkbox component, checked rather than value for your component property is much more meaningful.

  3. You should be passing in the checked state to your checked property, not a random variable called checked.

In this short example I've created my own Checkbox component to show you how it all fits together.

const { Component } = React;

class Example extends Component {

  constructor() {
    super();
    this.state = { checked: false };
  }

  // Set the state, and the call the callback
  // function to log the new state
  handleChange = (e) => {
    this.setState({ checked: e.target.checked }, () => {
      console.log(this.state.checked);
    });
  }

  render() {
    return (
      <div>
        <Checkbox
          label="test"
          checked={this.state.checked}
          handleChange={this.handleChange}
        />
      </div>
    );
  }

};

class Checkbox extends Component {

  render() {
  
    const {
      checked,
      label,
      handleChange
    } = this.props;

    return (
      <fieldset>
        <legend>{label}</legend>
        <input
          type="checkbox"
          checked={checked}
          onChange={handleChange}
        />
      </fieldset>
    )
  
  }

}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
input[type="checkbox"]:hover { cursor: pointer; } 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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