wavesoft / dot-dom

.dom is a tiny (512 byte) template engine that uses virtual DOM and some of react principles
Apache License 2.0
809 stars 53 forks source link

Listener like/array based life-cycle hooks #34

Closed SilentCicero closed 7 years ago

SilentCicero commented 7 years ago

You can now add multiple lifecycle hooks per component. The life cycle variables are arrays, which functions can be pushed too.

SilentCicero commented 7 years ago

Got it done, 510 bytes with safe scoping @kumavis @wavesoft =D

styfle commented 7 years ago

Are you gonna add examples to the README how to use this?

SilentCicero commented 7 years ago

@styfle Yes, we will need to change the ReadMe.

Mainly..

function MyComponent(props, state, setState, hooks){
    hooks.m.push(() => console.log('component did mount life cycle hook'));
    hooks.d.push(() => console.log('did update life cycle hook'));
    hooks.u.push(() => console.log('did unload life cycle hook'));

    return H('div', 'My component body');
}

R(H(MyComponent), document.body);

So the life cycle hooks are added by pushing a method to the hook arrays. This allows for both, multiple hooks to be added, and for things like higher-order components to be created.

Right now we are dealing with a scope/state sharing component design problem (it can be avoided however, if every component returns at least a single element as their child's first node (that isn't another component). We are figuring out a way to collapse this now. Gonna be hard though, we only have a few bytes left ;)

wavesoft commented 7 years ago

Ow yeah ✨ Awesome work @SilentCicero :1st_place_medal: I am going to give it a spin and merge 👍

wavesoft commented 7 years ago

Hmm @SilentCicero what's your "build" command? Because when I try to minify this it gets down to 520 bytes :thinking:

SilentCicero commented 7 years ago

@wavesoft just using npm run build:uglify, 508 bytes by the latest count.

SilentCicero commented 7 years ago

@wavesoft I really want to solve that higher component problem. See gitter.

wavesoft commented 7 years ago

👷 LET'S MERGE THIS ! 👷

Merge