odoo / owl

OWL: A web framework for structured, dynamic and maintainable applications
https://odoo.github.io/owl/
Other
1.14k stars 344 forks source link

t-ref is no longer supported on components. Consider exposing only the public part of the component's API through a callback prop. #1589

Closed pumppi closed 7 months ago

pumppi commented 7 months ago

Hi,

I want to access to component from template. How to do that.

<BaseField t-ref="text" type="'text'" label="'text'" />

This t-ref isn't supported but theres now any examples how to implement callback pro. My goal is to access component from the parent component to get values. For example field values. Now I need to do props drilling if want to access for example onchange listener.

pumppi commented 7 months ago

Looks like the solution is for example this kind of?

<BaseField componentMounted="(component) => this.setComponent(component)" />

sdegueldre commented 7 months ago

The reason component ref were removed is because it's generally a bad idea to give unrestricted access to a component from outside: owl manages the lifecycle and lifetimes of component and passing component instances around is a bad idea and prone to leaking memory or operating on unmounted components. It's pretty easy to devise something similar to what's done in the React ecosystem: an imperative handle, where a component can opt into exposing a specific subset of its own API. Here is a link to such an implementation on the owl playground.

pumppi commented 7 months ago

Thanks