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

195
Visualizações
Mutating the DOM in React

So usually there are really easy ways to avoid mutating your DOM directly from React but unfortunately I'm not in one of those situations right now. I'm using an API that then embeds a lot of content on to my page into a div with an id of dashboard.

I need to manipulate the content on the page and move certain elements around and things. Currently I add an event listener to an element that exists before I call the API, with useEffect() like so:

useEffect(() => {
    /*
     Some code here...
    */

    const setupDashboard = (dashboard) => {

      document.querySelector('#run').addEventListener('click', () => {
        dashboard.send('dashboard:run')
        console.log("Run clicked")
      })

      console.log(document.querySelector("[data-title='States']"))
    }

    LookerEmbedSDK.createDashboardWithId(114)
      .appendTo('#dashboard')
      .on('dashboard:run:start',
        /*() => updateState('#dashboard-state', 'Running')*/
      )
      .on('dashboard:run:complete',
        /*() => updateState('#dashboard-state', 'Done')*/
      )
      .build()
      .connect()
      .then(setupDashboard)
      .catch((error) => {
        console.error('An unexpected error occurred', error)
      })
    });

The piece of code that adds the event handler to the button works, but the next console statement returns null. So it seems like there should be a better way to manipulate the DOM especially since I'm going to completely change it with all my modifications. I'll need to use all sorts of query selectors, deletions, appending and more. What's the best practice for all that.

Edit: to be clear, after the page loads if I run that line in the console it gets the element

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found the answer to my particular situation. In my case, the second query selector doesn't work because the embedded element is in an iframe that represents a second document and is therefore inaccessible.

However, if you're working with react and you need a way to mutate the DOM might I suggest using

ReactDOM.shouldComponentUpdate = () => false

found in this article https://www.reddit.com/r/reactjs/comments/2riuwa/mutating_reacts_dom/

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