React 的 Hooks API 已针对 Web 组件实现
React 的 Hooks API,但适用于标准 Web 组件和 lit-html 或 hyperHTML。
<html lang="en">
<my-counter></my-counter>
<script type="module">
import { html } from 'https://unpkg.com/lit?module';
import { component, useState } from 'https://unpkg.com/haunted/haunted.js';
function Counter() {
const [count, setCount] = useState(0);
return html`
<button type="button" @click=${() => setCount(count + 1)}>
Increment
</button>
`;
}
customElements.define('my-counter', component(Counter));
</script>
</html>更多集成示例可在此 gist中找到。
Haunted 支持与 React Hooks 相同的 API。希望这样能让你通过在你的打包器配置中别名化包名,来复用 npm 上可用的 hooks。
目前 Haunted 支持以下 hooks:
// Or another renderer, see Guides
type Renderer = (element: Element) => TemplateResult;
interface Options {
baseElement: HTMLElement;
observedAttributes: string[];
useShadowDOM: 布尔值
}
声明函数组件
renderer: Renderer,
options: Options
):元素;
声明函数组件<BaseElement = HTMLElement>
renderer: Renderer,
baseElement: BaseElement,
选项:Optio
[Read the Docs](https://hauntedhooks.netlify.app)暂无开放 Issues,或尚未同步最近议题。