Let’s say there is a <div> somewhere in HTML file:
I want to pass an attribute data-person-name from that <div> having id root,to react element
<div id="root" data-person-name="John"></div>
const element = <h1>Hello,{data-person-name}</h1>;
ReactDOM.render(element, document.getElementById('root'));
Please suggest the right way to do that.
personNameis an invalid attribute fordivelements. If you want to add non-DOM information to elements, use adata-*attribute (e.g.,data-person-name).document.getElementById("root")and get its attribute value viagetAttribute("attribute-name-goes-here"). But this feels like an X/Y problem. Why do you want to take an attribute value and use it in a React element?data-person-name.So for that purpose, I need to do that