WebReflection / heresy

React-like Custom Elements via V1 API builtin extends.
https://medium.com/@WebReflection/any-holy-grail-for-web-components-c3d4973f3f3f
ISC License
276 stars 16 forks source link

React like hooks for the render method 🎉 #14

Closed WebReflection closed 4 years ago

WebReflection commented 4 years ago
const {define, render, html, useState} = heresy;

define('Welcome', {
  hooks: true, // enable hooks within the render method
  extends: 'element',
  style: comp => `
    ${comp} {
      font-size: 16px;
      font-family: sans-serif;
      font-weight: bold;
    }
    ${comp} > u {
      font-weight: normal;
    }`
  ,
  render() {
    const [clicks, setState] = useState(0);
    this.html`Hello <u>${navigator.userAgent}</u>
    <hr/>
    <button onclick=${() => setState(clicks + 1)}>clicks ${clicks}</button>`;
  }
});

render(document.body, html`<Welcome/>`);