@bettercms-ai/react

中文文档
0.5.0发布于 2026/11/05TypeScriptES Module近 30 天下载 2073

BetterCMS for React apps (Vite and other client-rendered React): provider, hooks, draft mode by link, and the section and field components.

原始使用文档
文档目录3 节
  1. Your own components, editable
  2. Live
  3. Draft mode

@bettercms-ai/react

BetterCMS for client-rendered React apps (Vite, CRA, any SPA). For Next.js use @bettercms-ai/next, for Astro @bettercms-ai/astro.

npm i @bettercms-ai/react
VITE_BCMS_API_URL=https://api.bettercms.ai
VITE_BCMS_WORKSPACE=your-workspace
VITE_BCMS_PROJECT_ID=your-project-id
// main.tsx
import { BcmsProvider } from "@bettercms-ai/react";
createRoot(el).render(<BcmsProvider env={import.meta.env}><App /></BcmsProvider>);

// a page
import { BcmsBlocks, usePage, useComponents } from "@bettercms-ai/react";
export function Home() {
  const { data: page } = usePage("home");
  const { data: components } = useComponents();
  return page ? <BcmsBlocks blocks={page.blocks} components={components} /> : null;
}

Hooks: usePage(slug), useEntry(slug), useEntries(opts), useLayout(), useComponents(), useBcmsQuery(key, read); each returns { data, error, loading }.

Components (the same markup as @bettercms-ai/next): BcmsBlocks, BcmsSections (inside <BcmsProvider> and given no blocks, <BcmsSections page="home"> reads the page's order itself, so the app's own sections render in the order the CMS page lists them), BcmsSection, BcmsField, BcmsImage, BcmsButton, BcmsForm, BcmsSearch, and BcmsDraftBridge, which lets the editor re-render drafts with your own components.

Your own components, editable

Spread a binding onto your own elements and the visual editor binds them:

import { entryAttrs, entryField, fieldAttrs, sectionRootAttrs } from "@bettercms-ai/react";

<h1 {...fieldAttrs("hero-title", page.fieldStyles)}>{title}</h1>
<article {...entryAttrs(post)}><h3 {...entryField(post, "title")}>{post.data.title}</h3></article>

Live

Every hook re-reads when content is published (pass liveSrc, or set VITE_BCMS_LIVE_URL), and when the visual editor asks the page to re-render. Re-reading keeps the current page on screen until the new data arrives. useBcms().refresh() re-reads on demand. In draft mode the provider also stamps <html data-bcms-draft="1"> and loads the click-to-edit overlay.

Draft mode

No server needed. The editor's "Preview on site" link opens /api/draft?token=…&redirect=/page on your site; an SPA host serves your app there, and <BcmsProvider> checks the token with BetterCMS, keeps it for the tab and moves to redirect. Reads then return drafts. useBcms().exitDraft() leaves draft mode. Set draftPath on the provider if your app uses another path (and the same path in the project's integration settings).

Your host must serve the app at that path, as it does for any client-side route: Netlify /* /index.html 200 in _redirects, a Vercel rewrite of /api/draft to /index.html (Vercel keeps /api for functions otherwise), and in vite dev no proxy on /api. GitHub Pages cannot rewrite: set draftPath to a path your app serves (/draft) and the same path in the integration settings. A token the backend refuses, or a check that cannot reach it, leaves the app published; an expired token is dropped the next time the app loads.

版本与发布记录

@bettercms-ai/react 共发布 12 个版本,首次发布于 2026/10/19;过去 12 个月发布了 12 个版本。

版本发布日期距今
0.5.0latest2026/11/052 天前
0.4.22026/11/026 天前
0.4.12026/10/2613 天前
0.4.02026/10/2613 天前
0.3.52026/10/2513 天前
0.3.42026/10/2514 天前

关于 @bettercms-ai/react 的常见问题

@bettercms-ai/react 是什么?

BetterCMS for React apps (Vite and other client-rendered React): provider, hooks, draft mode by link, and the section and field components.

如何安装 @bettercms-ai/react?

在项目目录运行 npm install @bettercms-ai/react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bettercms-ai/react 支持 TypeScript 吗?

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

@bettercms-ai/react 支持 ESM 还是 CommonJS?

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

@bettercms-ai/react 可以商用吗?

@bettercms-ai/react 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@bettercms-ai/react 的下载量有多少?

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

@bettercms-ai/react 还在维护吗?

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