Skip to content

Commit

Permalink
fix: ensure observer component name is only set when configurable (#3831
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kitsuned authored Feb 22, 2024
1 parent b970cbb commit 1b8ab19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-cheetahs-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx-react-lite": patch
---

fix: ensure observer component name is only set when configurable
16 changes: 11 additions & 5 deletions packages/mobx-react-lite/src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { useObserver } from "./useObserver"
let warnObserverOptionsDeprecated = true

const hasSymbol = typeof Symbol === "function" && Symbol.for
const isFunctionNameConfigurable =
Object.getOwnPropertyDescriptor(() => {}, "name")?.configurable ?? false

// Using react-is had some issues (and operates on elements, not on types), see #608 / #609
const ReactForwardRefSymbol = hasSymbol
? Symbol.for("react.forward_ref")
Expand Down Expand Up @@ -106,11 +109,14 @@ export function observer<P extends object, TRef = {}>(

// Inherit original name and displayName, see #3438
;(observerComponent as React.FunctionComponent).displayName = baseComponent.displayName
Object.defineProperty(observerComponent, "name", {
value: baseComponent.name,
writable: true,
configurable: true
})

if (isFunctionNameConfigurable) {
Object.defineProperty(observerComponent, "name", {
value: baseComponent.name,
writable: true,
configurable: true
})
}

// Support legacy context: `contextTypes` must be applied before `memo`
if ((baseComponent as any).contextTypes) {
Expand Down

0 comments on commit 1b8ab19

Please sign in to comment.