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

285
Views
React-Hook Form: ¿Cómo convierto el valor de la cantidad en un número que está dentro de useFieldArray?

Ya he declarado la input para recibir solo números:

 <input type="number" name={`order[${nestIndex}].variantion[${k}].qty`} ref={register({ required: true })} defaultValue={item.qty} style={{ marginRight: "25px" }} />

Pero esto aún se guardaría como una cadena. ¿Cómo puedo convertir el valor de la quantity en un número o si pudiera analizarlo en un número entero?

El campo de entrada de cantidad está en nestedFieldArray

 export default ({ nestIndex, control, register }) => { const { fields, remove, append } = useFieldArray({ control, name: `order[${nestIndex}].variation` }); return ( <div> {fields.map((item, k) => { return ( <div key={item.id} style={{ marginLeft: 20 }}> <label>{k + 1}</label> <input type="number" name={`order[${nestIndex}].variantion[${k}].qty`} ref={register({ required: true })} defaultValue={item.qty} style={{ marginRight: "25px" }} /> <Size name={`order[${nestIndex}].variantion[${k}].color`} menuItems={colorItems} refer={register({ required: true })} defaultValue={item.color} control={control} /> </div> ); })} <hr /> </div> ); };

Recreé esto en: https://codesandbox.io/s/react-hook-form-data-in-step-1-and-step-2-with-nestedarray-7hyksh?file=/src/nestedFieldArray.js: 485-1532

Así es como se ven los datos en la consola:

ingrese la descripción de la imagen aquí

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

0

En el envío del controlador, puede cambiar la cantidad del producto a un número entero usando parseInt () e insertarlo en la nueva matriz secundaria y, después de que todos los detalles del producto lo conviertan, puede insertarlo en la nueva matriz principal.

 const onSubmit = (data) => { const newOrder = []; data.order.forEach(({ product, variantion }) => { const newVariantion = []; variantion.forEach(({ qty, color }) => { newVariantion.push({ qty: parseInt(qty), color }); }); newOrder.push({ product, variantion: newVariantion }); }); action(newOrder); console.log(newOrder); push("/step2"); };
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!