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

106
Visualizações
How do i set sate based on the condition in checkbox Change function reactjs

In the checkbox the default value is null and I am setting it as true when required but also I thought of one more condition that needs to be set when the checkbox is already selected I want to unselect it and set the value as false instead of null.

Assume alwaysSelected -if this is selected it is true(onload) based on this condition i should update the state

this.state = {
checkbox: null,
}
this.props.alwaysSelected=true

<Checkbox
onChange={this.ChangeValue}
/>

ChangeValue= (event) => {
this.setState(checboxState=> ({
checkbox: checboxState.checkbox ? null : true
});
})

Just say prevState.checkbox -true on a load of the page then it just gives null but need to set it as false

Option tried is setState callback and componentDidUpdate but failed to acheive.

can we do this in another approach??

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can pass the checkbox state to the Checkbox component as value and change the event:

ChangeValue = () => this.setState({checkbox:!checkbox});

or if event returns true/false

 ChangeValue = event => this.setState({checkbox:!event});
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