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

100
Visualizações
React memo creates rerender

I'm having an issue with react memo when using nextjs. In the _app e.g. I have a button imported:

import { ChildComponent } from './ChildComponent';

export const Button = ({ classN }: { classN?: string }) => {
  const [counter, setCounter] = useState(1);
  
  const Parent = () => {
     <button onClick={() => setCounter(counter + 1)}>Click me</button>
  }
  
  return (
    <div>
      {counter}
      <Parent />
      <ChildComponent />
    </div>
  );
};

Child component:

import React from 'react';

export const ChildComponent = React.memo(
  () => {
    React.useEffect(() => {
      console.log('rerender child component');
    }, []);

    return <p>Prevent rerender</p>;
  },
  () => false
);

I made one working in React couldn't figure it out in my own app: https://codesandbox.io/s/objective-goldwasser-83vb4?file=/src/ChildComponent.js

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

0

The second argument of React.memo() must be a function that returns true if the component don't need to be rerendered and false otherwise - or in the original definition, if the old props and the new props are equal or not.

So, in your code, the solution should be just change the second argument to:

export const ChildComponent = React.memo(
  () => { ... },
  // this
  () => true
);

Which is gonna tell React that "the props didn't change and thus don't need to rerender this component".

about 4 years ago · Juan Pablo Isaza Relatório

0

So my issue was that I made a function called Button and returned inside a button or Link. So I had a mouseEnter inside the button which would update the state and handle the function outside the function. Kinda embarrassing. This fixed it. So the only change was I moved usestate and handlemousehover inside the button function.

const Button = () => {
    const [hover, setHover] = useState(false);

    const handleMouseHover = (e: React.MouseEvent<HTMLElement>) => {
      if (e.type === 'mouseenter') {
        setHover(true);
      } else if (e.type === 'mouseleave') setHover(false);
    };

    return (
      <StyledPrimaryButton
        onMouseEnter={(e) => handleMouseHover(e)}
        onMouseLeave={(e) => handleMouseHover(e)}
      >
        <StyledTypography
          tag="span"
          size="typo-20"
        >
          {title}
        </StyledTypography>
        <ChildComponent />
      </StyledPrimaryButton>
    );
  };
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