Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
¿Cómo puedo restablecer reaccionar desde todos los campos de entrada?

Usé la biblioteca React Hooks Form para mi proyecto. Pero no puedo restablecer después de enviar datos. A continuación, he añadido mi código.

 const AddItem = () => { const { register, handleSubmit } = useForm(); const onSubmit = (data) =>{ const url = `http://localhost:5000/products`; fetch(url,{ method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(data) }) .then(res=>res.json()) .then(data=>{ toast("successfully added product"); }); }; return ( <div className='container border-2'> <PageTitle title={"Add Product"}></PageTitle> <div className=""> <h4 className='mt-5'>Add Items</h4><hr /> <form onSubmit={handleSubmit(onSubmit)} className="d-flex flex-column w-50 mx-auto"> <input className='mb-3 p-1' type="text" name="itemName" placeholder='Item Name' {...register("itemName", { required: true })}/> <input className='mb-3 p-1' type="text" name="imgLink" placeholder='Image URL' {...register("imgLink", { required: true })}/> <input className='mb-3 p-1' type="textarea" name="description" placeholder='Product Description' {...register("description", { required: true })}/> <input className='mb-3 p-1' type="number" name="price" placeholder='Product Price' {...register("price", { required: true })}/> <input className='mb-3 p-1' type="number" name="quantity" placeholder='Quantity' {...register("quantity", { required: true })}/> <input className='mb-3 p-1' type="text" name="supplierName" placeholder='Supplier Name' {...register("supplierName", { required: true })}/> <input type="submit" value="Add Item" className='btn btn-primary' /> </form> </div> <ToastContainer /> </div> ); };

Sin el formulario React hooks, solía restablecer el formulario data.target.reset(); . ¿Cómo puedo restablecer todos los campos de entrada? Por favor, si sabes esto, di algo. Gracias por adelantado...

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Para restablecer los datos del formulario usando react-hook-form,

  1. Primero, importe el módulo de reinicio desde el formulario de gancho de reacción como se muestra a continuación
 const { register, handleSubmit, reset } = useForm();
  1. Y luego use el módulo de reinicio
 const onSubmit = (data) =>{ ... reset() // Reset All field reset({"itemName": "item"}) // Reset with values }
about 4 years ago · Juan Pablo Isaza Report

0

Puede restablecer todo el formulario o campos parciales utilizando el enlace de restablecimiento devuelto por useForm()

 const { register, handleSubmit, reset } = useForm(); const onSubmit = (data) => { //... reset(); };

Echa un vistazo a la documentación aquí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!