Middle+
What arguments does React.memo accept besides the component itself?
sobes.tech AI
Answer from AI
React.memo takes two arguments:
- Component, which needs to be memoized — usually a functional component.
- Comparison function (optional) — takes two props objects (prevProps and nextProps) and returns
trueif the props are considered equal and a re-render is not needed, orfalseif the component should be updated.
Example of usage with a comparison function:
const MyComponent = React.memo(function MyComponent(props) {
// rendering
}, (prevProps, nextProps) => {
// compare only the necessary fields
return prevProps.value === nextProps.value;
});
If the comparison function is not provided, React.memo performs a shallow comparison of props by default.