Slide 1
-----------
Part 1: By now, you learned how to build pretty complex applications. But, as the complexity of your application grows, you need to be aware of how to optimise it when it starts to become sluggish.
Slide 2
-----------
Part 1: Often, the problem is the number of unnecessary re-renders. Let’s first create a scenario where a child component re-renders every time the parent component re-renders, even if its input (props) remains unchanged.
Part 2: How this works? The ParentComponent holds a count state and provides a button to increment this count. Each time the button is clicked, setCount triggers a re-render of the ParentComponent
Part 3: The NonPureComponent is a simple functional component that displays a message prop. Even though the message prop remains constant, the child component is re-rendered every time the parent updates its state.
Part 4: When you interact with the button, you will notice that console-log statement runs every time, even though the message prop hasn’t changed. This happens because React functional components are not "pure" by default—they re-render when their parent re-renders. While a simple example like this doesn’t cause noticeable issues, in larger applications, unnecessary renders can lead to Performance Bottlenecks and Unintended Side Effects.
Slide 3
-----------
Part 1: We can solve this problem by wrapping the child component with React-memo, a higher-order component that optimises functional components by preventing re-renders when props don’t change.
Part 2: How does it work? By wrapping the functional component with React-memo, React compares the new props with the previous props.
If the props haven’t changed, React skips re-rendering the component. In our example, when you increment the count in ParentComponent, PureComponent no longer re-renders because its message prop doesn’t change.
Part 3: But, if we include the value of the count as a prop of our Pure component ...
Part 4: ... as expected, clicking on the button will trigger a re-render for each click.
By now, you learned how to build pretty complex applications. But, as the
complexity of your application grows, you need to be aware of how to optimise it
when it starts to become sluggish. Often, the proble is the number of
unnecessary re-renders. Let’s first create a scenario where a child component
re-renders every time the parent component re-renders, even if its input (props)
remains unchanged.
NON-PURE COMPONENT EXAMPLE
import React from 'react';
const NonPureComponent = ({ message }) => {
console.log('NonPureComponent rendered');
return <div>{message}</div>;
};
const ParentComponent = () => {
const [count, setCount] = React.useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>Increment Count: {count}</button>
<NonPureComponent message="Hello from Non-Pure Component!" />
</div>
);
};
export default ParentComponent;
HOW THIS WORKS
1. Parent Component:
* The ParentComponent holds a count state and provides a button to increment
this count. Each time the button is clicked, setCount triggers a re-render
of the ParentComponent.
2. Child Component:
* The NonPureComponent is a simple functional component that displays a
message prop.
* Even though the message prop remains constant, the child component is
re-rendered every time the parent updates its state.
When you interact with the button, you will notice that
console.log('NonPureComponent rendered') runs every time, even though the
message prop hasn’t changed. This happens because React functional components
are not "pure" by default—they re-render when their parent re-renders.
WHY THIS IS A PROBLEM
While a simple example like this doesn’t cause noticeable issues, in larger
applications, unnecessary renders can lead to:
* Performance Bottlenecks:
* If a component performs expensive computations or renders a large DOM
subtree, re-rendering it unnecessarily can slow down your app.
* Unintended Side Effects:
* If the component uses useEffect or other hooks tied to rendering, these
might be triggered even when they aren’t needed, leading to bugs or
inefficient behavior.
--------------------------------------------------------------------------------
INTRODUCING REACT.MEMO: MAKING THE COMPONENT PURE
We can solve this problem by wrapping the child component with React.memo, a
higher-order component (HOC) that optimizes functional components by preventing
re-renders when props don’t change.
PURE COMPONENT WITH REACT.MEMO
import React from 'react';
const PureComponent = React.memo(({ message }) => {
console.log('PureComponent rendered');
return <div>{message}</div>;
});
const ParentComponent = () => {
const [count, setCount] = React.useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>Increment Count: {count}</button>
<PureComponent message="Hello from Pure Component!" />
</div>
);
};
export default ParentComponent;
--------------------------------------------------------------------------------
HOW REACT.MEMO WORKS
1. Memoizing the Component:
* By wrapping the functional component with React.memo, React compares the
new props with the previous props.
* If the props haven’t changed, React skips re-rendering the component.
2. Behavior Change:
* In this example, when you increment the count in ParentComponent,
PureComponent no longer re-renders because its message prop doesn’t
change.
You can observe this in the console—console.log('PureComponent rendered') only
runs on the initial render and does not trigger again, even when the parent
updates.
--------------------------------------------------------------------------------
WHY USING REACT.MEMO IS IMPORTANT
1. PERFORMANCE OPTIMIZATION
By reducing unnecessary renders, React.memo helps optimize your application’s
performance. In real-world applications with deep component trees or
computationally expensive operations, avoiding redundant renders can make a
significant difference.
2. IMPROVED PREDICTABILITY
Using React.memo enforces a more predictable rendering behavior. Developers can
reason more easily about when a component will re-render, based on changes to
its props.
3. RESOURCE EFFICIENCY
For components that perform heavy rendering logic (e.g., complex calculations,
rendering lists with thousands of items), React.memo ensures that they only
render when their inputs (props) change, reducing the CPU workload.
--------------------------------------------------------------------------------
WHEN TO USE REACT.MEMO
While React.memo is a powerful tool, it’s essential to use it judiciously:
* Use it for Components that Don’t Change Often:
* Components that only re-render when their props change are the best
candidates.
* Avoid Overuse:
* Wrapping every component in React.memo can add unnecessary complexity and
marginal performance gains in simple components.
--------------------------------------------------------------------------------
CONCLUSION
React.memo is a simple yet powerful optimization tool for functional components.
It allows you to transform impure components, which re-render unnecessarily,
into pure components that only re-render when required. This not only improves
your app's performance but also makes its behavior more predictable and
resource-efficient. By understanding and strategically using React.memo, you can
build React applications that scale effectively without compromising on
performance or maintainability.
Maggie is a generative AI that can help you understand the course content better. You can ask her questions about the lecture, and she will try to answer them. You can also see the questions asked by other students and her responses.
Join the discussion to ask questions, share your thoughts, and discuss with other learners