Before you go, check out these stories!

Hackernoon logoA Beginner's Guide to Performance Optimization Using React.memo() by@Kiran

A Beginner's Guide to Performance Optimization Using React.memo()

Author profile picture


Content Writer at Truemark Technology. Company Website Link -

is a higher-order component provided by React that will return a memoized version of the component that only changes if one of the props has changed. It is the same as
but instead of classes
is used for functional components.

Why use React.memo?

memoizes the rendered output then skips unnecessary rendering. This helps to prevent unnecessary re-rendering of components and computations needed for component rendering.

React.memo in action

As an example implementation lets create a component which will:

  • Greet user
  • Show number of times user has greeted
  • Let user greet using button

Let’s create and add a function/method on

component that does the work of simulating some heavy computations while rendering the component.

// userGreeting.js

const UserGreeting = () => {
  const getUserName = () => {
    let i = 0;
    while (i < 3000000000) i++;

    return 'John Doe';

  return <div>Hello {getUserName()},</div>;

components will show the count and increment greet count on click respectively and do not have any heavy computations.

// greetingCount.js

const GreetingCount = ({ count }) => (
  return <div>You greeted me {count} times.</div>;
// button.js

const Button = ({ title, onClick }) => (
  <button onClick={onClick}>{title}</button>

And the parent component will import all these components and have a method to update the greetings count.


const App = () => {
  const [greetCount, setGreetCount] = useState(0);
  const onGreet = () => {
    setGreetCount(greetCount + 1);

  return (
    <div className='App'>
      <UserGreeting />
      <GreetingCount count={greetCount} />
      <Button title='Hi' onClick={onGreet} />


As you can see that there is a delay for certain intervals before the UI updates after the button is clicked. This is because when we click on the button the state changes so every component is rerendered and the

component is rerendered as well. The
method is executed again due to a re-render of the
component thus causing a delay in UI update.


So the solution for the above problem is to use

. The
method will memoize the component and does a shallow comparison of the component and since none of the props in

component has been changed, it will skip re-rendering of this component. This will prevent the recomputation during the render and the UI updates quickly. For this we will wrap the component with
and export it.

const UserGreeting = () => {
  // code here};

export default React.memo(UserGreeting);


As you can see now that the component does not re-render the

component and the UI is updated without any delay.

You can find the complete example on CodeSandbox

This post was first published on DevPostbyTruemark.

Author profile picture


Read my stories

Content Writer at Truemark Technology. Company Website Link -


Join Hacker Noon

Create your free account to unlock your custom reading experience.