From 8a1ff856043d59396f623f8ca209822b1331d85f Mon Sep 17 00:00:00 2001 From: urugator <11457665+urugator@users.noreply.github.com> Date: Mon, 6 Sep 2021 16:45:36 +0200 Subject: [PATCH] Missing render on observer throws (#3103) --- .changeset/blue-hairs-itch.md | 5 +++++ packages/mobx-react/__tests__/observer.test.tsx | 9 +++++++++ packages/mobx-react/src/observerClass.ts | 8 ++++++++ 3 files changed, 22 insertions(+) create mode 100644 .changeset/blue-hairs-itch.md diff --git a/.changeset/blue-hairs-itch.md b/.changeset/blue-hairs-itch.md new file mode 100644 index 000000000..d2a9244a8 --- /dev/null +++ b/.changeset/blue-hairs-itch.md @@ -0,0 +1,5 @@ +--- +"mobx-react": patch +--- + +Missing `render` on component prototype throws. diff --git a/packages/mobx-react/__tests__/observer.test.tsx b/packages/mobx-react/__tests__/observer.test.tsx index 9a4d36310..147024156 100644 --- a/packages/mobx-react/__tests__/observer.test.tsx +++ b/packages/mobx-react/__tests__/observer.test.tsx @@ -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 { + render = function() { + return
+ } + } + expect(() => observer(Component)).toThrow(); +}) diff --git a/packages/mobx-react/src/observerClass.ts b/packages/mobx-react/src/observerClass.ts index 7fddc23c4..66bd0513a 100644 --- a/packages/mobx-react/src/observerClass.ts +++ b/packages/mobx-react/src/observerClass.ts @@ -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) }