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

145
Visualizações
react-bootstrap form switch button

I can't seem to figure out why I am not able to set the default value for react-bootstrap switch to false (off). It seems the only time the value will change is when I trigger the onChange event handler. Am I missing something? Here is the switch portion in my form:

<Form.Group as={Row} className="mb-3">
  <Col sm={10}>
    <Form.Check
      type="switch"
      id="custom-switch"
      label="Enable GTC"
      defaultChecked="false"
      onChange={(e) => setField('gtc', e.target.checked)}
    />
  </Col>
</Form.Group>

Here is how I am handling the state for the form.

  const setField = (field, value) => {
    setForm({
      ...form,
      [field]: value,
    });
  };

I would think `defaultChecked="false" would do the trick but it does not.

Thanks!

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

0

Remove defaultChecked="false". By having it as a prop, defaultChecked is set to true.

about 4 years ago · Juan Pablo Isaza Relatório

0

const [switchState, setSwitchState] = useState(false);

then assign the :

defaultChecked={switchState}

Now write change handler function

const handleChange=(e)=>{
   setField('gtc', e.target.checked)
   setSwitchState(!switchState)
    
}

final code:

<Form.Group as={Row} className="mb-3">
  <Col sm={10}>
    <Form.Check
      type="switch"
      id="custom-switch"
      label="Enable GTC"
      defaultChecked={switchState}
      onChange={handleChange}
    />
  </Col>
</Form.Group>
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