Skip to main content

How is React.memo() similar to shouldComponentUpdate()?

The general idea

Both React.memo() and shouldComponentUpdate() let you control whether a component should re-render when its props have not changed.

Both exist for performance optimization - to avoid unnecessary re-renders when a component receives the same data.


shouldComponentUpdate() - in class components

This is a lifecycle method that is called before a re-render:

javascript
class User extends React.Component { shouldComponentUpdate(nextProps, nextState) { // return false → React skips render() return nextProps.name !== this.props.name; } render() { console.log('render'); return <div>{this.props.name}</div>; } }

If the method returns:

  • true -> React calls render() and updates the component;
  • false -> React skips the render and does not touch the DOM.

React.memo() - in function components

It is a HOC (Higher-Order Component) that does the same thing for function components that shouldComponentUpdate does for class components.

javascript
const User = React.memo(function User({ name }) { console.log('render'); return <div>{name}</div>; });

By default React.memo does a shallow comparison of the props. If the old and new props are identical (by Object.is), the component will not be re-rendered.


In other words:

ComponentMechanismControl
ClassshouldComponentUpdate(nextProps, nextState)You return true / false
FunctionReact.memo(Component, areEqual?)The second argument is a props comparison function

Custom comparison in React.memo

If needed, you can explicitly specify custom comparison logic (the equivalent of a hand-written shouldComponentUpdate):

javascript
const User = React.memo( function User({ name, age }) { console.log('render'); return <div>{name} ({age})</div>; }, (prevProps, nextProps) => prevProps.age === nextProps.age // If true → do not update );

Here the areEqual(prevProps, nextProps) function returns:

  • true -> do not update (the equivalent of shouldComponentUpdate = false);
  • false -> update (the equivalent of shouldComponentUpdate = true).

Direct mapping of the logic

What it doesClass variantFunction variant
Checks if the props changedshouldComponentUpdate()React.memo()
Shallow comparison by defaultNo (only through PureComponent)Yes, built into React.memo()
Custom comparison logicshouldComponentUpdate(nextProps, nextState)(prevProps, nextProps) => boolean
Skips the re-renderreturn falseareEqual() → true

Example: equivalent code

Class component:

javascript
class Button extends React.Component { shouldComponentUpdate(nextProps) { return nextProps.label !== this.props.label; } render() { console.log('render'); return <button>{this.props.label}</button>; } }

Function component:

javascript
const Button = React.memo( ({ label }) => { console.log('render'); return <button>{label}</button>; }, (prev, next) => prev.label === next.label );

The behavior is exactly the same.


What is different

ParametershouldComponentUpdate()React.memo()
Applies toClass componentsFunction components
ControlInside the componentOutside, at the wrapping level
Compares stateYes (through nextState)No (only props)
Can be overriddenYesYes (through areEqual)
Shallow comparison by defaultNo (needs PureComponent)Yes
Where you write itInside the classOutside: React.memo(Component)

An optimization example

Without optimization:

javascript
function Parent({ user }) { return <Child user={user} />; } function Child({ user }) { console.log('render'); return <div>{user.name}</div>; }

Every time a new user object appears -> Child re-renders. With React.memo():

javascript
const Child = React.memo(({ user }) => { console.log('render'); return <div>{user.name}</div>; });

Now React compares the old and new user -> if the reference is the same, Child does not update.


Summary

React.memo()shouldComponentUpdate()

  • both exist to prevent unnecessary re-renders when the input data (props) has not changed.
Shared goalBehavior
Improving performanceSkipping a re-render if the data has not changed
Shallow comparison by defaultOnly in React.memo() and PureComponent
Flexibility through a custom comparisonAvailable in both

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.