如何在 hyperHTML 中重新实现 material.io 个组件?
How to reimplement material.io components in hyperHTML?
我正在查看 material.io 组件如何 implemented/integrated 使用 React 组件的 React 示例:
我想知道如何使用 hyperHTML 做同样的事情,因为 hyper.Component
中没有生命周期事件调用,例如 componentDidMount
或 componentWillUnmount
。
据说我可以在 render
调用之后执行此操作,但这通常是从组件外部调用的。
抱歉我现在才看到这个。
我想澄清几件事:
there are no lifecycle event calls within hyper.Component like componentDidMount or componentWillUnmount.
如果您尝试最新版本,或者说 1.10+,您将能够在每个 hyper.Component
class 中定义 onconnected(evt) { ... }
和 ondisconnected(evt) { ... }
方法定义。
这是一个相当新的功能,不幸的是还没有记录。但它提供了所有你需要的行为就像自定义元素 connectedCallback
和 disconnectedCallback
方法(记住,还有一个 HyperHTMLElement project 来创建真正的自定义元素)。
下面是一个基本的例子:
import {Component} from 'hyperhtml';
class Clock extends Component {
update() {
this.time = (new Date).toLocaleTimeString();
this.render();
}
// equivalent of Custom Elements connectedCallback
onconnected() {
console.log('start time');
this.update();
this.timer = setInterval(() => this.update(), 1000);
}
// equivalent of Custom Elements disconnectedCallback
ondisconnected() {
console.log('end time');
clearInterval(this.timer);
}
render() { return this.html`
<p
onconnected=${this}
ondisconnected=${this}
>
${this.time}
</p>`;
}
}
我希望这会给您足够的力量来复制 material 示例。
我想澄清的另一部分:
It was said that I can do that after render call but that is typically called from outside of component.
这不完全正确。是的,component.render()
会在需要时自动调用,但重要的是你 return.
我使用的代码与之前的 Clock
示例相同:
// same code as before
render() {
const p = this.html`
<p
onconnected=${this}
ondisconnected=${this}
>
${this.time}
</p>`;
// you have a <p> element here
// you can do whatever you want
// example:
p.addEventListener('click', console.log);
// now you should return it
return p;
}
// rest of the code
如您所见,您始终可以与渲染节点进行交互。毕竟,hyperHTML 所做的就是创建您编写的内容,仅此而已。
我希望这些说明能帮助您继续前进。
最后,在这里回答其他问题。
我正在查看 material.io 组件如何 implemented/integrated 使用 React 组件的 React 示例:
我想知道如何使用 hyperHTML 做同样的事情,因为 hyper.Component
中没有生命周期事件调用,例如 componentDidMount
或 componentWillUnmount
。
据说我可以在 render
调用之后执行此操作,但这通常是从组件外部调用的。
抱歉我现在才看到这个。
我想澄清几件事:
there are no lifecycle event calls within hyper.Component like componentDidMount or componentWillUnmount.
如果您尝试最新版本,或者说 1.10+,您将能够在每个 hyper.Component
class 中定义 onconnected(evt) { ... }
和 ondisconnected(evt) { ... }
方法定义。
这是一个相当新的功能,不幸的是还没有记录。但它提供了所有你需要的行为就像自定义元素 connectedCallback
和 disconnectedCallback
方法(记住,还有一个 HyperHTMLElement project 来创建真正的自定义元素)。
下面是一个基本的例子:
import {Component} from 'hyperhtml';
class Clock extends Component {
update() {
this.time = (new Date).toLocaleTimeString();
this.render();
}
// equivalent of Custom Elements connectedCallback
onconnected() {
console.log('start time');
this.update();
this.timer = setInterval(() => this.update(), 1000);
}
// equivalent of Custom Elements disconnectedCallback
ondisconnected() {
console.log('end time');
clearInterval(this.timer);
}
render() { return this.html`
<p
onconnected=${this}
ondisconnected=${this}
>
${this.time}
</p>`;
}
}
我希望这会给您足够的力量来复制 material 示例。
我想澄清的另一部分:
It was said that I can do that after render call but that is typically called from outside of component.
这不完全正确。是的,component.render()
会在需要时自动调用,但重要的是你 return.
我使用的代码与之前的 Clock
示例相同:
// same code as before
render() {
const p = this.html`
<p
onconnected=${this}
ondisconnected=${this}
>
${this.time}
</p>`;
// you have a <p> element here
// you can do whatever you want
// example:
p.addEventListener('click', console.log);
// now you should return it
return p;
}
// rest of the code
如您所见,您始终可以与渲染节点进行交互。毕竟,hyperHTML 所做的就是创建您编写的内容,仅此而已。
我希望这些说明能帮助您继续前进。 最后,在这里回答其他问题。