Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

69
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda