Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf] props (reactive object) should not be observed outside of rendering #1601

Closed
seb-odoo opened this issue Mar 29, 2024 · 3 comments
Closed

Comments

@seb-odoo
Copy link
Contributor

seb-odoo commented Mar 29, 2024

What is observed?
Reactive props are always observed, even if they are used outside of the render.

Why is it bad?
This leads to extra renders when the value changes, which can be problematic for performance if the render is costly or happening many times.

Solution?
When the value of a props is used outside of rendering, it should not be observed and its change should not trigger a re-render.

Example
playground

Prints: 1, then 2
Expected: only 1

import { Component, useState, mount, onWillRender } from "@odoo/owl";

class Greeter extends Component {
    static template = "Greeter";
    
    setup() {
        this.renderCount = 0;
        onWillRender(() => { this.renderCount++ }); // to notice there is an extra render (it will print 2)
        setTimeout(() => this.props.obj.name, 150); // accessing value outside of render
    }
}

// Main root component
class Root extends Component {
    static components = { Greeter };
    static template = "Root"

    setup() {
        this.state = useState({ obj: { name: 'Hi'} });
        setTimeout(() => this.state.obj.name = "Hello", 300); // changing value, after it was accessed outside of render in child
    }
    
}

mount(Root, document.body, { templates: TEMPLATES, dev: true });
<templates>
  <div t-name="Greeter">
    <t t-esc="renderCount"/>
  </div>

  <t t-name="Root">
    <Greeter obj="state.obj"/>
  </t>
</templates>
@seb-odoo seb-odoo changed the title props (reactive object) should not be observed outside of rendering [performance] props (reactive object) should not be observed outside of rendering Mar 29, 2024
@seb-odoo seb-odoo changed the title [performance] props (reactive object) should not be observed outside of rendering [perf] props (reactive object) should not be observed outside of rendering Mar 29, 2024
@ged-odoo
Copy link
Contributor

yep, we know, and i think that we agree.

@seb-odoo
Copy link
Contributor Author

seb-odoo commented Apr 2, 2024

duplicate #1574 ?

@ged-odoo
Copy link
Contributor

ged-odoo commented Apr 2, 2024

good catch. yes, exactly. closing this then. Thanks for caring enough to make an issue

@ged-odoo ged-odoo closed this as completed Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants