0
import React, {useState} from 'react';
import './button.css';
{
   /* <input type="text" placeholder="SCORE" name="score" />
<input type="text" placeholder="PERCENT" name="percent" /> */
}
function DynamicInput() {
   const [score, setScore] = useState([]);
   const [percent, setPercent] = useState([]);
   const [valuescore, setValuescore] = useState([]);
   const [valuepercent, setValuepercent] = useState([]);
   const LIMIT = 3;

   //ADD SCORE INPUT
   function handleScore() {
      if (score.length < LIMIT) {
         setScore([...score, <input key={score.length} type="text" placeholder={'SCORE'} name="score" />]);
      }
      console.log(score.length);
   }

   //ADD PERCENT INPUT
   function handlePercent() {
      if (percent.length < LIMIT) {
         setPercent([...percent, <input key={percent.length} type="text" placeholder={'PERCENT'} name="percent" />]);
      }
   }

   //REMOVE INPUT
   function handleRemove() {
      if (score.length > 0) {
         const newInputs = [...score];
         const newPercent = [...percent];
         newInputs.pop();
         newPercent.pop();
         setScore(newInputs);
         setPercent(newPercent);
      } else {
         alert('No puedes retirar mas inputs');
      }
   }

   //GET INPUT VALUE
   const getInput = (ev) => {
      ev.preventDefault();
      const inputs = ev.target.elements.score;
      console.log(inputs);
      const newScores = Array.from(inputs).map((input) => input.value);
      console.log(newScores);
      setValuescore(newScores);
      console.log(valuescore);
   };

   // GET PERCENT VALUE
   const getPercent = (ev) => {
      ev.preventDefault();
      const inputs = ev.target.elements.score;
      const newScores = Array.from(inputs).map((input) => input.value);
      setValuepercent(newScores);
   };


   return (
      <div>
         <div className="Binputs">
            <form id="valueScore" className="content" onSubmit={getInput}>
               {score.map((input) => input)}
            </form>
            <form id="valuePercent" className="content" onSubmit={getPercent}>
               {percent.map((input) => input)}
            </form>
         </div>
         <button
            onClick={() => {
               handleScore();
               // handlePercent();
            }}
         >
            Agregar
         </button>
         <button onClick={handleRemove}>Retirar</button>
         <button type="submit" form="valueScore">
            Calcular
         </button>
      </div>
   );
}

export default DynamicInput;

It happens that the first entry is not saved in the array until there are 2 inputs

the first input is not saved

first image with 1 input

but with 2 or more inputs it is saved correctly

second image with 2 inputs

.I'm looking for it to be saved even though it only has 1 input and not necessarily having to add 2 inputs

Noisi
  • 1
  • why you are writing code like this : setPercent([...percent, ]); – HimonHimu May 14 '23 at 06:38
  • @HimonHimu - nothing wrong with keeping a dynamic array of JSX in state. JSX is just synactic sugar for a JS object, after all. The OP appears at first glance to be rendering these correctly (although `{score.map((input) => input)}` is a very verbose way of just writing `{score}`). – Robin Zigmond May 14 '23 at 08:13
  • to the OP: is there a problem with how your app is actually behaving, or is it just the console output that's confusing you? Calling eg `setScore` will *not* update the value of `score` in a way that can be observed in the immediately following code - so `score.length` on the next line will still be the "previous length". See [here](https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately) for a detailed look at this. Note that this isn't actually a problem - if there is something wrong other than in the console, please give more detail. – Robin Zigmond May 14 '23 at 08:16

0 Answers0