'Add entries dynamically

I have an array with 3 objects, each one is information of an input (text, select and radio) I have a component that has 3 buttons, when pressing each one it should add an input according to the text of the button (text, select, radio ), I think about going through the array and creating it according to the type, but I can't find the way, how would I do it? in the same way I must eliminate each of these entries created



Solution 1:[1]

Give the button a value of whatever type you want to add

<button value = 'radio' onClick = {handleClick}>Add Radio </button>

and then just add to an array an input with the same type

const [inputs, setInputs] = useState([])

const handleClick = (e)=>{
  setInputs(prev=>[...prev, <input type = {e.target.value} />])
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WebbH