Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda