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

159
Visualizações
How to avoid re-renders in React when using conditional Render and children node prop

I have a component that I reuse most of its logic. I'm looking to avoid re-renders on its children components which happen every time I hover over the parent:

const ReusableComponent = ({ conditional }) => {
  const [isHovered, setIsHovered] = useState(false);

  const AnotherReusableComponent = ({ children }) => (
    <div>{children}</div>
  );

  const renderComponent = () => {
    if (conditional) {
      return <ComponentA />;
    };

    return <ComponentB />;
  };

  return (
    <div>
      <title 
        onMouseEnter={() => setIsHovered(true)}
        onMouseLeave={() => setIsHovered(false)}
      >
        Menu
      </title>
      <div className={isHovered ? 'oneClass' : 'otherClass'}>
        <AnotherReusableComponent>
          {renderComponent()}
        </AnotherReusableComponent>
      </div>
    </div>
  );
}

Notes:

  • ComponentA and ComponentB are shown/hidden depending on className
  • onHover event toggles className
  • Tried memo, didn't work.
  • The re-render happens onHover
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

  1. First, Components should never be declared inside other components, since they will be recreated at each render unless you memoize them with useMemo.
  2. Even if you declare it outside you will experience a re-render of <AnotherReusableComponent> and its children when isHovered changes, hence you need to wrap it into React.memo() to ensure it won't re-render unless its props change:
 const AnotherReusableComponent = React.memo(({ children }) => (
    <div>{children}</div>
  ));

const ComponentA = React.memo(() => (
    <div>A</div>
  ));

const ComponentB = React.memo(() => (
    <div>B</div>
  ));

const ReusableComponent = ({ condition }) => {
  const [isHovered, setIsHovered] = useState(false);

  return (
    <div>
      <title 
        onMouseEnter={() => setIsHovered(true)}
        onMouseLeave={() => setIsHovered(false)}
      >
        Menu
      </title>
      <div className={isHovered ? 'oneClass' : 'otherClass'}>
        <AnotherReusableComponent>
          {condition ? <ComponentA/> : <ComponentB/>}
        </AnotherReusableComponent>
      </div>
    </div>
  );
}


This should avoid the re-render of AnotherReusableComponent and its children unless condition changes.

about 4 years ago · Santiago Gelvez 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