MCPcopy
hub / github.com/mobxjs/mobx / makeClassComponentObserver

Function makeClassComponentObserver

packages/mobx-react/src/observerClass.ts:55–174  ·  view source on GitHub ↗
(
    componentClass: ComponentClass<any, any>
)

Source from the content-addressed store, hash-verified

53}
54
55export function makeClassComponentObserver(
56 componentClass: ComponentClass<any, any>
57): ComponentClass<any, any> {
58 const { prototype } = componentClass
59
60 if (componentClass[isMobXReactObserverSymbol]) {
61 const displayName = getDisplayName(componentClass)
62 throw new Error(
63 `The provided component class (${displayName}) has already been declared as an observer component.`
64 )
65 } else {
66 componentClass[isMobXReactObserverSymbol] = true
67 }
68
69 if (prototype.componentWillReact) {
70 throw new Error("The componentWillReact life-cycle event is no longer supported")
71 }
72 if (componentClass["__proto__"] !== PureComponent) {
73 if (!prototype.shouldComponentUpdate) {
74 prototype.shouldComponentUpdate = observerSCU
75 } else if (prototype.shouldComponentUpdate !== observerSCU) {
76 // n.b. unequal check, instead of existence check, as @observer might be on superclass as well
77 throw new Error(
78 "It is not allowed to use shouldComponentUpdate in observer based components."
79 )
80 }
81 }
82
83 if (__DEV__) {
84 Object.defineProperties(prototype, observablePropDescriptors)
85 }
86
87 const originalRender = prototype.render
88 if (typeof originalRender !== "function") {
89 const displayName = getDisplayName(componentClass)
90 throw new Error(
91 `[mobx-react] class component (${displayName}) is missing \`render\` method.` +
92 `\n\`observer\` requires \`render\` being a function defined on prototype.` +
93 `\n\`render = () => {}\` or \`render = function() {}\` is not supported.`
94 )
95 }
96
97 prototype.render = function () {
98 Object.defineProperty(this, "render", {
99 // There is no safe way to replace render, therefore it's forbidden.
100 configurable: false,
101 writable: false,
102 value: isUsingStaticRendering()
103 ? originalRender
104 : createReactiveRender.call(this, originalRender)
105 })
106 return this.render()
107 }
108
109 const originalComponentDidMount = prototype.componentDidMount
110 prototype.componentDidMount = function () {
111 if (__DEV__ && this.componentDidMount !== Object.getPrototypeOf(this).componentDidMount) {
112 const displayName = getDisplayName(componentClass)

Callers 1

observerFunction · 0.90

Calls 7

isUsingStaticRenderingFunction · 0.90
patchFunction · 0.90
getDisplayNameFunction · 0.85
unregisterMethod · 0.80
getAdministrationFunction · 0.70
renderMethod · 0.65
disposeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…