I want to append a div with two text fields whenever the user clicks a button.
I'm trying to do so by using useState and pushing the new JSX element to an array.
But I'm having a couple of problems:
- When clicking the button the first time, instead of the new elements in the DOM, I see the number 2, which seems to be the length of the array.
- By clicking the button again, I get the error
linksArray.push is not a function.
function LinkFieldsSet() {
return (
<div>
<TextField label="Text" />
<TextField label="URL" />
</div>
)
}
function Links() {
const [{linksArray, keyNum}, setState] = React.useState({
linksArray: [<LinkFieldsSet key={1} />],
keyNum: 1,
});
const handleAddClick = () => {
setState({
keyNum: keyNum + 1,
linksArray: linksArray.push(<LinkFieldsSet key={keyNum} />)
});
};
return (
<div>
{linksArray}
<AddRowsButton action={handleAddClick} />
</div>
)
}