@appsemble/preact

中文文档
0.40.9发布于 2026/10/30LGPL-3.0-onlyTypeScriptES Module零依赖构建来源已验证近 30 天下载 1939

Build your own blocks using Preact

原始使用文档
文档目录7 节
  1. Table of Contents
  2. Installation
  3. Usage
  4. bootstrap()
  5. useBlock()
  6. <FormattedMessage />
  7. License

Appsemble Preact SDK

Build your own blocks using Preact

npm GitLab CI Prettier

Table of Contents

Installation

npm install @appsemble/preact preact

Usage

This package integrates Preact with @appsemble/sdk. Please read the documentation for a better understanding of how to use it.

bootstrap()

The bootstrap function takes a Preact component as an argument and renders. It takes the Appsemble block context as its props, extended with the ready() function. The ready() function needs to be called when the block is done rendering. Actions won’t be finalized for any blocks on the page, until all blocks are ready.

import { bootstrap } from '@appsemble/preact';
import { useEffect } from 'preact/hooks';

bootstrap(({ actions }) => {
  useEffect(() => {
    ready();
  }, []);

  return (
    <button onClick={() => actions.onClick()} type="button">
      Hello world!
    </button>
  );
});

Note: The script will be loaded only once. The component is then bootstrapped for every instance of your block type that is loaded by the app creator.

useBlock()

Get the block context. This is available for components that are rendered within a subtree of a component rendered by bootstrap

import { useBlock } from '@appsemble/preact';
import { type VNode } from 'preact';

export function MyButton(): VNode {
  const { actions } = useBlock();

  return (
    <button onClick={() => actions.onClick()} type="button">
      Hello world!
    </button>
  );
}

<FormattedMessage />

This helper component renders a translated message ID.

import { FormattedMessage } from '@appsemble/preact';
import { type VNode } from 'preact';

interface MyButtonProps {
  readonly name: string;
}

export function MyButton({ name }: MyButtonProps): VNode {
  return (
    <button type="button">
      <FormattedMessage id="myButtonLabel" values={{ name }} />
    </button>
  );
}

License

LGPL-3.0-only © Appsemble

版本与发布记录

@appsemble/preact 共发布 367 个版本,首次发布于 2020/03/21;过去 12 个月发布了 62 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.40.9latest2026/10/309 天前
0.40.82026/10/309 天前
0.40.72026/10/2910 天前
0.40.7-node-brotli.0next2026/10/2712 天前
0.40.62026/10/2415 天前
0.40.52026/10/1623 天前

关于 @appsemble/preact 的常见问题

@appsemble/preact 是什么?

Build your own blocks using Preact

如何安装 @appsemble/preact?

在项目目录运行 npm install @appsemble/preact,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:preact(^10)、@appsemble/sdk(0.40.9)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@appsemble/preact 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@appsemble/preact 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@appsemble/preact 可以商用吗?

@appsemble/preact 使用 LGPL-3.0-only 许可。弱 Copyleft:作为库被调用时通常不要求开源你自己的代码,但修改库本身需以 LGPL 开源;前端打包合并后的适用边界存在不同理解。以上为概括,不构成法律意见,请以许可原文为准。

@appsemble/preact 的下载量有多少?

根据 npm 官方下载统计,@appsemble/preact 在 2026/10/07 – 2026/11/05 这 30 天内被下载 1,939 次,日均约 65 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appsemble/preact 还在维护吗?

最新版本 0.40.9 发布于 2026/10/30(9 天前);至今共发布 367 个版本,其中过去 12 个月发布了 62 个;首次发布于 2020/03/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。