Skip to content

Commit

Permalink
[FIX] crash when rendering component before mounting
Browse files Browse the repository at this point in the history
This commit makes sure that Owl does not crash when a component is
created, then updated (for example, with a (observed) state change), and
then, some moment later, mounted.

The initial render is not useful, because it is not linked to a mounting
action anyway.  And it caused issues such as a crash when Owl tried to
patch it to a non existing target
  • Loading branch information
ged-odoo authored and aab-odoo committed Jan 19, 2021
1 parent 71f5450 commit dfc7825
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ export class Component<Props extends {} = any, T extends Env = Env> {
async render(force: boolean = false): Promise<void> {
const __owl__ = this.__owl__;
const currentFiber = __owl__.currentFiber;
if (!__owl__.vnode && !currentFiber) {
return;
}
if (currentFiber && !currentFiber.isRendered && !currentFiber.isCompleted) {
return scheduler.addFiber(currentFiber.root);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/component/un_mounting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,22 @@ describe("unmounting and remounting", () => {
expect(detachedDiv.innerHTML).toBe("<div>2</div>");
});

test("change state and render while not mounted ", async () => {
class App extends Component {
static template = xml`<div><t t-esc="state.val"/></div>`;
state = useState({ val: 1 });
}

const app = new App(null);

app.state.val = 2; // will call the render method (before being mounted)
await nextTick();

await app.mount(fixture);

expect(fixture.innerHTML).toBe("<div>2</div>");
});

test("destroy and change state after mounted in detached dom", async () => {
class App extends Component {
static template = xml`<div><t t-esc="state.val"/></div>`;
Expand Down

0 comments on commit dfc7825

Please sign in to comment.