Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support displayName on anonymous components #3192

Merged
merged 10 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix
  • Loading branch information
urugator committed Nov 24, 2021
commit db0ad6d9c07e71dfc0bbc9c0e5279100de820ac7
5 changes: 5 additions & 0 deletions .changeset/warm-foxes-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx-react-lite": patch
---

Support customizing `displayName` on anonymous components [#2721](https://github.com/mobxjs/mobx/issues/2721).
5 changes: 2 additions & 3 deletions packages/mobx-react-lite/__tests__/observer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,7 @@ it("should hoist known statics only", () => {
MyHipsterComponent.compare = "Nope!"
MyHipsterComponent.render = "Nope!"

const wrapped = observer(MyHipsterComponent)
expect(wrapped.displayName).toBe("MyHipsterComponent")
const wrapped = observer(MyHipsterComponent)
expect(wrapped.randomStaticThing).toEqual(3)
expect(wrapped.defaultProps).toEqual({ x: 3 })
expect(wrapped.propTypes).toEqual({ x: isNumber })
Expand All @@ -576,7 +575,7 @@ it("should have the correct displayName", () => {
const TestComponent = observer(function MyComponent() {
return null
})

expect((TestComponent as any).type.displayName).toBe("MyComponent")
})

Expand Down
15 changes: 11 additions & 4 deletions packages/mobx-react-lite/src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export function observer<P extends object, TRef = {}>(
const wrappedComponent = (props: P, ref: React.Ref<TRef>) => {
return useObserver(() => baseComponent(props, ref), baseComponentName)
}
wrappedComponent.displayName = baseComponentName

// Don't set `displayName` for anonymous components,
// so the `displayName` can be customized by user, see #2721.
if (baseComponentName != '') {
wrappedComponent.displayName = baseComponentName
}

// Support legacy context: `contextTypes` must be applied before `memo`
if ((baseComponent as any).contextTypes) {
Expand All @@ -78,12 +83,11 @@ export function observer<P extends object, TRef = {}>(
}

copyStaticProperties(baseComponent, memoComponent)
memoComponent.displayName = baseComponentName

if ("production" !== process.env.NODE_ENV) {
Object.defineProperty(memoComponent, 'contextTypes', {
set() {
throw new Error(`[mobx-react-lite] \`${this.displayName || 'Component'}.contextTypes\` must be set before applying \`observer\`.`);
throw new Error(`[mobx-react-lite] \`${this.displayName || this.type?.displayName || 'Component'}.contextTypes\` must be set before applying \`observer\`.`);
}
})
}
Expand All @@ -96,7 +100,10 @@ const hoistBlackList: any = {
$$typeof: true,
render: true,
compare: true,
type: true
type: true,
// Don't redefine `displayName`,
// it's defined as getter-setter pair on `memo` (see #2721).
displayName: true,
}

function copyStaticProperties(base: any, target: any) {
Expand Down