Re:How Does React Tell a Class from a Function?
by brlewis on Monday December 03, 2018 @12:33PM
(#18590742)
Can't you achieve the same result by leaving only view logic in the component, and having the data-related logic, including the fact that data has changed in a way that may require redraw, handled in a separate object? Here's how I'd do it in Mithril/TSX:
class WidthStore {
width: stream.Stream<number>;
constructor() {
this.width = stream(window.innerWidth);
window.addEventListener('resize', () => {
this.width(window.innerWidth);
m.redraw();
});
}
}
const widthStore: WidthStore = new WidthStore();
class MyResponsiveComponent {
view(vnode) {
return <p>Window width is {widthStore.width()}</p>;
}
}
https://gist.github.com/gaearon/cb5add26336003ed8c0004c4ba82...
Without hooks, that example would require adding a method (or two) to the component class. With custom hooks, that code can go into a separate module.
Component bloat has been a problem for us, and if hooks can enable components to focus solely on render logic, I'd say that's an improvement.