Skip to content

Instantly share code, notes, and snippets.

@sergey-shpak
Created June 29, 2019 12:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergey-shpak/c1e0db3d52019eecb0b5717e8cbf00ad to your computer and use it in GitHub Desktop.
Save sergey-shpak/c1e0db3d52019eecb0b5717e8cbf00ad to your computer and use it in GitHub Desktop.
Hyperapp#2 lifecycle events helper
import { h } from 'hyperapp'
import Lifecycle, { withChildLifeCycle } from 'lifecycle'
// 'customEvent.detail' contains element triggered lifecycle event
// please use effects to properly manipulate dom element
const action = (state, customEvent) => state
// and somewhere
<Lifecycle>
<div oncreated={ action }>created</div>
</Lifecycle>
// or
{ withChildLifeCycle(<form>
<input type="text" oncreated={ action } />
</form>) }
import { h } from 'hyperapp'
const wrap = (method, eventName) => function (el){
const event = new CustomEvent(eventName, { detail: el })
setTimeout(() => el.dispatchEvent(event))
return Object.getPrototypeOf(this)[method].call(this, el)
}
export const withChildLifeCycle = node => {
return { ...node, props: {
appendChild: wrap('appendChild', 'create'),
removeChild: wrap('removeChild', 'remove'),
...node.props
}}
}
export default (props, child) =>
withChildLifeCycle(<div {...props} >{ child }</div>)
@websemantics
Copy link

Also see, hyperapp-lifecycle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment