Created
October 6, 2020 18:20
-
-
Save nikparo/b6b6a8bfa3891093c3efe73d7cb36f46 to your computer and use it in GitHub Desktop.
React.memo: Shallowly compare props except for `style`, which is best shallowly compared on its own.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import shallowEqual from 'shallowequal'; | |
function customizer(valueA, valueB, propName) { | |
if (propName === 'style') { | |
return shallowEqual(valueA, valueB); | |
} | |
return undefined; | |
} | |
function styleAndPropsAreEqual(prevProps, nextProps) { | |
return shallowEqual(prevProps, nextProps, customizer); | |
} | |
// Shallowly compare props except for 'style', which is best shallowly compared on its own. | |
export function styleMemo(Component) { | |
return React.memo(Component, styleAndPropsAreEqual); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment