Skip to content

Commit

Permalink
Missing render on observer throws (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator authored Sep 6, 2021
1 parent f966873 commit 8a1ff85
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-hairs-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx-react": patch
---

Missing `render` on component prototype throws.
9 changes: 9 additions & 0 deletions packages/mobx-react/__tests__/observer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,12 @@ test("Redeclaring an existing observer component as an observer should log a war
observer(AlreadyObserver)
expect(warn).toHaveBeenCalled()
})

test("Missing render should throw", () => {
class Component extends React.Component<any, any> {
render = function() {
return <div />
}
}
expect(() => observer(Component)).toThrow();
})
8 changes: 8 additions & 0 deletions packages/mobx-react/src/observerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export function makeClassComponentObserver(
makeObservableProp(target, "state")

const baseRender = target.render
if (typeof baseRender !== 'function') {
const displayName = getDisplayName(target)
throw new Error(
`[mobx-react] class component (${displayName}) is missing \`render\` method.`
+ `\n\`observer\` requires \`render\` being a function defined on prototype.`
+ `\n\`render = () => {}\` or \`render = function() {}\` is not supported.`
)
}
target.render = function () {
return makeComponentReactive.call(this, baseRender)
}
Expand Down

0 comments on commit 8a1ff85

Please sign in to comment.