Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

70
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda