I'm trying to implement an add/remove fields from an array. I have my state hook setup, and should be able to click the add button to add a number to the emails array. It should then loop through the array of emails, and render an input for every value in the array. Each button looped over should have a remove button that removes it's specific value from the array.
I'm not able to add or remove elements:
const [emails, setEmails] = useState([]);
function RemoveEmail(i){
const index = emails.indexOf(i);
emails.splice(index, 1)
}
{emails.length > 0 && emails.map((email, i) => <p>{email}<button onClick={() => RemoveEmail(i)}>Remove</button></p>)}
<Button variant="outlined" size="small" onClick={() => setEmails(emails + 1)}>+ Add another</Button>
Update
Modified the add button so I can add values, but can't get the slice to remove a given element from the array:
<Button variant="outlined" size="small" onClick={() => setEmails([...emails, 1])}>+ Add another</Button>