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

141
Visualizações
Quiero restablecer mi formulario después de hacer clic en enviar en reactjs. He intentado hacer otro método. Pero no funciona
import React,{ useState} from 'react'; import { Button, Checkbox, Form } from 'semantic-ui-react'; import axios from 'axios'; const Create = () => { const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [checkbox, setCheckBox] = useState(false);

Aquí estoy enviando datos a una API simulada que creé.

 const postData = () =>{ axios.post(`https://61cb2af8194ffe0017788c01.mockapi.io/fakeData`,{ firstName, lastName, checkbox }) }

Este es el método que creé para restablecer el formulario pero no funciona.

 const resetForm = () => { postData(); setFirstName(" "); setLastName(" "); setCheckBox(false); }

Este es mi formulario donde, al hacer clic, llamo a la función resetForm pero no se restablece, envía los datos pero no restablece el formulario.

 return( <div> <Form> <Form.Field> <label>First Name</label> <input id="f1" placeholder='First Name' onChange={(e)=>setFirstName(e.target.value) } /> </Form.Field> <Form.Field> <label>Last Name</label> <input id="last1" placeholder='Last Name' onChange={(e)=>setLastName(e.target.value)}/> </Form.Field> <Form.Field> <Checkbox id="c1" label='I agree to the Terms and Conditions' onChange={(e)=>setCheckBox(!checkbox)}/> </Form.Field> <Button type='submit' onClick={resetForm}>Submit</Button> </Form> <br></br> <Button onClick={()=>navigate(-1)}>Go Back</Button> </div> ) } export default Create;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

En realidad, restablecerá el formulario, el problema es que no usa los componentes controlados para mostrar el último valor de actualización en la interfaz de usuario

debe vincular el valor de esta manera:

 <input type="text" value={this.state.value} onChange={this.handleChange} />

Puede consultar el documento aquí: https://reactjs.org/docs/forms.html

about 4 years ago · Juan Pablo Isaza Relatório

0

Puede restablecer su formulario usando el método nativo form.reset() .

 const Create = () => { const ref = React.useRef(null); const resetForm = () => ref.current.reset(); return ( <div> <Form ref={ref}> <Form.Field> <label>First Name</label> <input id="f1" placeholder="First Name" onChange={(e) => setFirstName(e.target.value)} /> </Form.Field> <Form.Field> <label>Last Name</label> <input id="last1" placeholder="Last Name" onChange={(e) => setLastName(e.target.value)} /> </Form.Field> <Form.Field> <Checkbox id="c1" label="I agree to the Terms and Conditions" onChange={(e) => setCheckBox(!checkbox)} /> </Form.Field> <Button type="submit" onClick={resetForm}> Submit </Button> </Form> <br></br> <Button onClick={() => navigate(-1)}>Go Back</Button> </div> ); }; export default Create;
about 4 years ago · Juan Pablo Isaza Relatório

0

Para eso, debe establecer la propiedad de value en su input . Prueba esto

 const Create = () => { const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [checkbox, setCheckBox] = useState(false); const postData = () => { console.log(firstName, lastName, checkbox); }; const resetForm = () => { postData(); setFirstName(" "); setLastName(" "); setCheckBox(false); }; return ( <div> <Form> <Form.Field> <label>First Name</label> <input id="f1" placeholder="First Name" value={firstName} onChange={(e) => setFirstName(e.target.value)} /> </Form.Field> <Form.Field> <label>Last Name</label> <input id="last1" placeholder="Last Name" value={lastName} onChange={(e) => setLastName(e.target.value)} /> </Form.Field> <Form.Field> <Checkbox id="c1" label="I agree to the Terms and Conditions" checked={checkbox} onChange={(e) => setCheckBox(!checkbox)} /> </Form.Field> <Button type="submit" onClick={resetForm}> Submit </Button> </Form> </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