How to attach id’s to components dynamically in React

I am building a React App for which I need to know which component is clicked.

document.addEventListener("click", function(e){
  console.log(e.target)
})

gives the target element, but there could be multiple component having same className and content inside id.

So, i was thinking about adding ‘ID’ to the component.

The way it works is..

  • if ID doesn’t exist then add ID to the component

Since ID’s are usually unique this would help me in determining component. I am not sure how I can do it using React.

Can someone help me please