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

164
Views
No puedo acceder al índice de la función de mapa dentro de la llamada de función en el botón (onClick()), que está dentro del alcance de map(), el código está debajo

Estado inicial

 const [variation, setVariation] = useState([ { name: "", value: [""], }, ]);

función al hacer clic en el botón

 const addValueField = (e, index) => { e.preventDefault(); const field = [...variation]; field[index].value.push(""); setVariation(field); };

representación html:

 <div> {variation.map((value, index) => { return ( <div className="form-control"> <input key={index} className="bg-base-200 m-1 shadow appearance-none border rounded text-gray-700 leading-tight focus:outline-none focus:shadow-outline" type="text" placeholder="Enter Variation Type" /> <div className="form-control"> {variation[index].value.map((value, i) => { return ( <input type="text" key={i} className="bg-base-200 m-1 shadow appearance-none border rounded text-gray-700 leading-tight focus:outline-none focus:shadow-outline" placeholder="Enter Value" /> ); })} <button onClick={(e, index) => addValueField(e, index)}> Add another value </button> </div> </div> ); })} </div>

Cuando se llama a addValueField(e,index) al hacer clic en el botón, no selecciona el índice del mapa principal(), pero el botón está dentro del alcance del mapa principal(), por lo que no pasa el índice a la función y se genera un error.

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

0

onClick no te da el index . Tienes que obtener el índice del map directamente si lo usas como

 <button onClick={(e) => addValueField(e, index)}> Add another value </button>;

y puede establecer el estado de variation como

 const addValueField = (e, index) => { e.preventDefault(); setVariation((state) => state.map((o, i) => (i === index ? { ...o, value: [...o.value, ""] } : o)) ); };
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!