Skip to content

Instantly share code, notes, and snippets.

View OrkhanAlikhanov's full-sized avatar
☝️
Be mindful of your Creator! We all will die soon...

Orkhan Alikhanov OrkhanAlikhanov

☝️
Be mindful of your Creator! We all will die soon...
View GitHub Profile
@slikts
slikts / react-memo-children.md
Last active April 27, 2024 02:44
Why using the `children` prop makes `React.memo()` not work

nelabs.dev

Why using the children prop makes React.memo() not work

I've recently ran into a pitfall of [React.memo()][memo] that seems generally overlooked; skimming over the top results in Google just finds it mentioned in passing in a [React issue][regit], but not in the [FAQ] or API [overview][react-api], and not in the articles that set out to explain React.memo() (at least the ones I looked at). The issue is specifically that nesting children defeats memoization, unless the children are just plain text. To give a simplified code example:

const Memoized = React.memo(({ children }) => (<div>{children}</div>));
// Won't ever re-render
<Memoized>bar</Memoized>
// Will re-render every time; the memoization does nothing
diff --git a/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js b/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js
index 441442f..a536c7a 100644
--- a/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js
+++ b/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js
@@ -735,7 +735,29 @@ var ExhaustiveDeps = {
},
enableDangerousAutofixThisMayCauseInfiniteLoops: {
type: 'boolean'
- }
+ },