Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!