diff --git a/src/component.ts b/src/component.ts index 7c6b89fb..39d6b526 100644 --- a/src/component.ts +++ b/src/component.ts @@ -35,7 +35,7 @@ export interface Meta { renderProps: Props | null; renderPromise: Promise | null; boundHandlers: { [key: number]: any }; - observer: Observer; + observer?: Observer; } const patch = init([sdListeners, sdAttrs, sdProps]); @@ -62,7 +62,7 @@ export class Component< } env: T; - state: State = {}; + state?: State; props: Props; refs: { [key: string]: Component | HTMLElement | undefined; @@ -120,8 +120,7 @@ export class Component< renderId: 1, renderPromise: null, renderProps: props || null, - boundHandlers: {}, - observer: new Observer() + boundHandlers: {} }; } @@ -366,13 +365,17 @@ export class Component< this.__owl__.renderId++; const promises: Promise[] = []; const template = this.inlineTemplate || this.template; - this.__owl__.observer.allowMutations = false; + if (this.__owl__.observer) { + this.__owl__.observer.allowMutations = false; + } let vnode = this.env.qweb.render(template, this, { promises, handlers: this.__owl__.boundHandlers, forceUpdate: force }); - this.__owl__.observer.allowMutations = true; + if (this.__owl__.observer) { + this.__owl__.observer.allowMutations = true; + } // this part is critical for the patching process to be done correctly. The // tricky part is that a child widget can be rerendered on its own, which @@ -422,7 +425,8 @@ export class Component< } _observeState() { - if (Object.keys(this.state).length) { + if (this.state) { + this.__owl__.observer = new Observer(); this.__owl__.observer.observe(this.state); this.__owl__.observer.notifyCB = this.render.bind(this); } diff --git a/tests/component.test.ts b/tests/component.test.ts index 0b8b3765..837a4db8 100644 --- a/tests/component.test.ts +++ b/tests/component.test.ts @@ -72,7 +72,7 @@ describe("basic widget properties", () => { test("props and state are properly defined", async () => { const widget = new Widget(env); expect(widget.props).toEqual({}); - expect(widget.state).toEqual({}); + expect(widget.state).toEqual(undefined); }); test("has no el after creation", async () => {