Skip to content

Commit

Permalink
Fix unnecessary top-level context changes (#1924)
Browse files Browse the repository at this point in the history
Reviewed-by: Titus Wormer <[email protected]>

Closes GH-1924.
  • Loading branch information
gaearon authored Feb 1, 2022
1 parent 9d5501b commit 9ca9d40
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/react/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ export function withMDXComponents(Component) {
*/
export function useMDXComponents(components) {
const contextComponents = React.useContext(MDXContext)

// Custom merge via a function prop
if (typeof components === 'function') {
return components(contextComponents)
}

return {...contextComponents, ...components}
// Memoize to avoid unnecessary top-level context changes
return React.useMemo(() => {
// Custom merge via a function prop
if (typeof components === 'function') {
return components(contextComponents)
}
return {...contextComponents, ...components}
}, [contextComponents, components])
}

/** @type {Components} */
const emptyObject = {}

/**
* Provider for MDX context
*
Expand All @@ -84,7 +88,7 @@ export function MDXProvider({components, children, disableParentContext}) {
let allComponents = useMDXComponents(components)

if (disableParentContext) {
allComponents = components || {}
allComponents = components || emptyObject
}

return React.createElement(
Expand Down

1 comment on commit 9ca9d40

@vercel
Copy link

@vercel vercel bot commented on 9ca9d40 Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.