Skip to content
This repository has been archived by the owner on Dec 31, 2020. It is now read-only.

Fix: observer should not modify original componentClass #523

Merged
merged 3 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,17 @@ export function observer(arg1, arg2) {
throw new Error("Please pass a valid component to 'observer'")
}

const target = componentClass.prototype || componentClass
const targetClass = class targetClassName extends componentClass {}
Copy link
Member

Choose a reason for hiding this comment

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

Wondering, will targetClassName now be the reported name in exceptions and React devtools and such?

Choose a reason for hiding this comment

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

It will, so it's worth implementing .displayName

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, just added displayName for targetClass

const target = targetClass.prototype || targetClass
Copy link
Member

Choose a reason for hiding this comment

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

Is the right branch ever taken?

mixinLifecycleEvents(target)
componentClass.isMobXReactObserver = true
targetClass.isMobXReactObserver = true
makeObservableProp(target, "props")
makeObservableProp(target, "state")
const baseRender = target.render
target.render = function() {
return makeComponentReactive.call(this, baseRender)
}
return componentClass
return targetClass
}

function mixinLifecycleEvents(target) {
Expand Down
67 changes: 67 additions & 0 deletions test/observer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,73 @@ describe("use Observer inject and render sugar should work ", () => {
})
})

describe("should not modify original componentClass", () => {
test("keep original at client", () => {
useStaticRendering(false)

class Origin extends React.Component {}

Origin.prototype.render = "DO_NOT_MODIFY"
observer(Origin)
expect(Origin.prototype.render).toBe("DO_NOT_MODIFY")
})

test("keep original at server", () => {
useStaticRendering(true)

class Origin extends React.Component {}

Origin.prototype.render = "DO_NOT_MODIFY"
observer(Origin)
expect(Origin.prototype.render).toBe("DO_NOT_MODIFY")
})

test("no exception: Maximum call stack size exceeded", () => {
// this test maybe a little slow (10s)

useStaticRendering(true)

class NameDisplayer extends Component {
render() {
return <h1>{this.props.name}</h1>
}
}

function getWraped() {
return inject(stores => ({
name: stores.userStore.name
}))(observer(NameDisplayer))
}

const user = mobx.observable({
name: "Noa"
})

class Name2 extends Component {
render() {
let UserNameDisplayer = getWraped()
return (
<div>
<UserNameDisplayer />
</div>
)
}
}

function getHtml() {
return ReactDOMServer.renderToString(
<Provider userStore={user}>
<Name2 />
</Provider>
)
}

for (let i = 0; i < 20000; i++) {
getHtml()
}
})
})

test("don't use PureComponent", () => {
const msg = []
const baseWarn = console.warn
Expand Down