3 ms·
This is my point about attacking composition with what we have for classes - because reuse and composition are problems not limited to UI widgets. Your query e
by spankalee 6y ago
This is my point about attacking composition with what we have for classes - because reuse and composition are problems not limited to UI widgets.
Your query example is easily handled by a mixin:
// Queryable provides this.data and this.loading and
// hooks into the lifecycle
class MyComponent extends Queryable(Component) {
query = 'some query';
render() {
// ...
}
}
You can also use helper objects:
class MyComponent extends Component {
query = new Query(this, 'some query');
render() {
// ...
}
connectedCallback() {
this.query.connect();
}
}
I understand that you have to wire up the lifecycle methods this way, but it's at least low-magic and easily understandable. I don't mind it.
And even though decorators aren't standard yet, they can be used for composition too.
My point is that these problems aren't unique to React and deserve patters and solutions for all JavaScript programming. React is statefull and object-based under the hood, but just trying to hide it.