24
loading...
This website collects cookies to deliver better user experience
React.memo()
. This tutorial will help you learn about React.memo()
. You will learn what it is, how it works and how to use it in your React apps.React.memo()
is. It is a high-order component (HOC). A higher-order component is a function that takes another component and returns a new component. You can think about HOC as a wrapper that transforms some component you give it into a new component.React.memo()
helps us increase performance of React apps by avoiding unnecessary renderings of components. Every time React has to decide whether to update the DOM, it compares the previous render with the new render. If these two renders are different, some data are different, React will re-render the DOM to update it.React.memo()
helps us make this process faster.React.memo()
three things will happen. First, React will render the component on the initial render as usually. After that, however, React will also memoize the component. React will store the result of that render in memory.memo()
, React will not automatically re-render the component. Instead, it will check if the new props of the component are the same as of the memoized component from the previous render.memo()
, works only with functional components. However, we can achieve this with class components if we use PureComponent.React.memo()
is that it is very easy to use. All we have to do is to take some functional component we want to memoize and wrap with memo()
. We can do this with new component we want to create as well as component that already exists.// Functional component without memo():
export const App = () => {
return (
<div className="App">
<h1>Hello world</h1>
</div>
)
}
// Functional component with memo():
// Import memo from React:
import { memo } from 'react'
// Wrap App component with memo():
export const App = memo(() => {
return (
<div className="App">
<h1>Hello world</h1>
</div>
)
})
// Create component and memoize it later:
// Import memo from React:
import { memo } from 'react'
// Create component:
const App = () => {
return (
<div className="App">
<h1>Hello world</h1>
</div>
)
}
// Memoize and export App component:
export const AppMemoized = memo(App)
// Compare "the same" objects:
console.log({ foo: 'foo' } === { foo: 'foo' })
// Output:
// false
// Or:
const obj1 = { foo: 'foo' }
const obj2 = { foo: 'foo' }
console.log(obj1 === obj2)
// Output:
// false
// Compare "the same" arrays:
console.log([1] === [1])
// Output:
// false
// Or:
const arr1 = [1]
const arr2 = [1]
console.log(arr1 === arr2)
// Output:
// false
// Use the same reference:
const obj1 = { foo: 'foo' }
const obj2 = obj1
console.log(obj1 === obj2)
// Output:
// true
const arr1 = [1]
const arr2 = arr1
console.log(arr1 === arr2)
// Output:
// true
React.memo()
with custom comparison function. This function comes as the second argument, right after the component we want to memoize.// Functional component with memo():
// Import memo from React:
import { memo } from 'react'
import { isEqual } from 'lodash'
// Create custom comparison function:
function compareProps(prevProps, nextProps) {
return isEqual(prevProps, nextProps)
}
// Wrap with memo() and use custom comparison function:
export const App = memo(() => {
return (
<div className="App">
<h1>Hello world</h1>
</div>
)
}, compareProps) // Pass compareProps as the 2nd argument
// Create component and memoize it later:
// Import memo from React:
import { memo } from 'react'
import { isEqual } from 'lodash'
// Create component:
const App = () => {
return (
<div className="App">
<h1>Hello world</h1>
</div>
)
}
// Memoize with custom comparison function:
export const AppMemoized = memo(App, compareProps) // Pass compareProps as the 2nd argument
true
if previous props and next props are equal. Otherwise, it should return false
.memo()
by default you should consider one thing. When you use it, React stores the result of rendering component in memory. If you decide to memoize a large number of components it will lead to more memory consumption.React.memo()
. Then, profile your app again and compare the results.React.memo()
can help you avoid these re-renders induced by parents. Second, try memoizing a component if the component always renders the same result given the same props. Third, your component renders a lot of UI. The more UI a component renders, the more expensive these renders usually are.React.memo()
can be very useful tool when we want to improve performance of our React apps. It makes it very easy to memoize components and avoid unnecessary re-renders. I hope that this tutorial helped you understand what memo()
is, how it works and how to use it.