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

130
Visualizações
How to detect if an HTML Element is the root of a React Component?

Is there a way to detect if an HTML Element is the root of any React component in a production app?

I'm not referring to detecting wether an element is the root of the entire React application, rather detecting if an element is the root of any component.

To illustrate:

Let's say we have the following React components:

const FooBar = () => {
  return (
    <div className="foobar">
      <span> foo </span>
      <button> bar </button>
    </div>
  );
}

const Container = () => {
  return (
    <div>
      <a className="john"> john </a>
      <a> doe </a>
      <FooBar />
    </div>
  );
}

That transpiles to the following HTML:

<div>
  <a class="john"> john </a>
  <a> doe </a>
  <div class="foobar">
    <span> foo </span>
    <button> bar </button>
  </div>
</div>

In a production deployment, is there a way to do this from a chrome extension (external script)? and how?

const foobarDiv = document.querySelector('div.foobar');
const span = document.querySelector('span');
const a = document.querySelector('a.john');
isReactComponentRoot(foobarDiv); // true
isReactComponentRoot(span); // false
isReactComponentRoot(a); // false
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I don't know if this will continue to be reliable in the future, but something that's worked for a while is to check if it has a _reactRootContainer property.

const root = document.querySelector('.react');
const App = () => {
    return <div className="child"></div>;
};

ReactDOM.render(<App />, root);

console.log(root.hasOwnProperty('_reactRootContainer'));
console.log(root.querySelector('.child').hasOwnProperty('_reactRootContainer'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></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