Comment

Comment by theteapot

> Hooks let you colocate logic that, in class components, would need to be split across multiple lifecycle methods.

I think the obvious OO alternative to hooks would have been this:

    class MyComponent {
        constructor() {
            this.attachBehaviour(new MyBehaviour(this));
            this.attachBehaviour(new MyOtherBehaviour(this));
        }

        render() { /* ... */ }
    }
    
    class MyBehaviour implements ComponentLifeCycleHooks {
        componentDidMount() { /* ... */ }
        componentWillUnmount() { /* ... */ }
        componentDidUpdate() { /* ... */ }
    }
Weird React didn't even seem to consider when they went to hooks. Would be possible to implement yourself though.

I'm not saying this is better than hooks / "composable" / "functional" API (I quite like Vue's composable API) but it's less of a departure from class based components.

parent theteapot 3 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton