Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

68
Views
how to add attribute without actually editing the HTML element in react

I have a below HTML component created in REACT which I cant edit directly. But I need to add one attribute to one of its HTML element. I can't use jquery as well other wise its easy to do with jQuery.

Below is the base HTML for table which I cant edit directly but I can just use this component in my code.

Challenge : I need to add attribute to the SVG element. e.g. -> data-id="1". Can it be done with CSS or any other way.

<TablePresenter>
<div>
   <svg>this is a actually a sort button</svg> 
   <div>Column 1 Name</div>
</div>
<div>
   <svg>this is a actually a sort button</svg> 
   <div>Column 2 Name</div>
</div>
</TablePresenter>

main file which I can edit is as below.

const MyComponent = () => {
some logic here...
Can we do something here may be CSS or any react hack to get underline component HTML change.
return(
  <TablePresenter></TablePresenter>
) 
}

export default MyComponent;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not a recommended way of doing, but using a ref you can achieve this, a sample below:

Add a wrapper div to your parent component (your own component which can be edited) like below and add a ref to it, that will helps you to get the DOM reference of the html inside the <TablePresenter>

const divRef= useRef<HTMLDivElement>(null);

return <div ref={divRef}>
    <TablePresenter></TablePresenter>
</div>

// This is an example of getting the reference of the svg.
divRef.current.firstElementChild.firstElementChild.firstElementChild

May be something like this:

const MyComponent = () => {
    const divRef = useRef<HTMLDivElement>(null);
    useEffect(()=>{
        divRef.current.firstElementChild.firstElementChild.firstElementChild.attributes["data-id"]=1
    }, [])

    return <div ref={divRef}>
         <TablePresenter></TablePresenter>
        <SuperChild />
    </div>
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!