Skip to content

Commit 9ca9d40

Browse files
authoredFeb 1, 2022
Fix unnecessary top-level context changes (#1924)
Reviewed-by: Titus Wormer <tituswormer@gmail.com> Closes GH-1924.
1 parent 9d5501b commit 9ca9d40

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed
 

Diff for: ‎packages/react/lib/index.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,19 @@ export function withMDXComponents(Component) {
6565
*/
6666
export function useMDXComponents(components) {
6767
const contextComponents = React.useContext(MDXContext)
68-
69-
// Custom merge via a function prop
70-
if (typeof components === 'function') {
71-
return components(contextComponents)
72-
}
73-
74-
return {...contextComponents, ...components}
68+
// Memoize to avoid unnecessary top-level context changes
69+
return React.useMemo(() => {
70+
// Custom merge via a function prop
71+
if (typeof components === 'function') {
72+
return components(contextComponents)
73+
}
74+
return {...contextComponents, ...components}
75+
}, [contextComponents, components])
7576
}
7677

78+
/** @type {Components} */
79+
const emptyObject = {}
80+
7781
/**
7882
* Provider for MDX context
7983
*
@@ -84,7 +88,7 @@ export function MDXProvider({components, children, disableParentContext}) {
8488
let allComponents = useMDXComponents(components)
8589

8690
if (disableParentContext) {
87-
allComponents = components || {}
91+
allComponents = components || emptyObject
8892
}
8993

9094
return React.createElement(

1 commit comments

Comments
 (1)

vercel[bot] commented on Feb 1, 2022

@vercel[bot]

Successfully deployed to the following URLs:

Please sign in to comment.