I am initialising myComponents
array with useState
hook
const [myComponents, setMyComponents] = useState([])
Then I am adding components to the array upon clicking of a button
setMyComponents([...myComponents, <MyComponent />])
MyComponent:
function MyComponent(props) {
return (
<div class="flex">
<button>DELETE</button>
<p>Some text...</p>
</div>
)
}
When any of the DELETE buttons pressed, relevant MyComponent from the myComponents
array should be removed. I cannot figure out how to get a reference to the component I have to delete. I cannot use the array index since when the first component is removed from the array, previous indices are no longer valid.
Please let me know how can I delete a particular MyComponent from the array so that it won't affect subsequent deletions.