@contentful/react-apps-toolkit

中文文档
3.0.1发布于 2026/10/26MITTypeScriptCommonJS近 30 天下载 144万

Toolkit for building a Contentful app in React

原始使用文档
文档目录8 节
  1. Installation
  2. Available features
  3. SDKProvider
  4. useSDK
  5. useCMA
  6. useFieldValue
  7. useAutoResizer
  8. Resources

React Toolkit for Contentful Apps

React Hooks for the App Framework offer a simple way to bring frequently needed functionality into your react based Contentful apps.

They can be used in apps created with create-contentful-app, as well as any other React app using Contentful's App SDK.

Installation

npm install @contentful/react-apps-toolkit
# or
yarn add @contentful/react-apps-toolkit

Available features

The following hooks and utilities are exported from the package:

SDKProvider

The SDKProvider is a wrapper component, which automatically makes the Contentful App SDK available to any child components using React Context. To use any of the hooks contained in this package, they must be wrapped in the <SDKProvider>, because all of the hooks depend on the App SDK.

Usage:

import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';

function ChildComponentUsingHook() {
  const sdk = useSDK<FieldExtensionSDK>();

  return <>App Id: {sdk.ids.app}</>;
}

function App() {
  return (
    <SDKProvider>
      <ChildComponentUsingHook />
    </SDKProvider>
  );
}

useSDK

useSDK returns an instance of the Contentful App SDK.

It must be wrapped it within the SDKProvider, otherwise, it will throw an error.

Usage:

import { SDKProvider, useSDK } from '@contentful/react-apps-toolkit';

function ComponentUsingSDK() {
  const sdk = useSDK<FieldExtensionSDK>();

  return <>App Id: {sdk.ids.app}</>;
}

function App() {
  return (
    <SDKProvider>
      <ChildComponentUsingSDK />
    </SDKProvider>
  );
}

useCMA

:warning: DEPRECATED If you are using App SDK v4.20 or greater use sdk.cma instead.

useCMA returns an initialized client for the Contentful Management API. This can be used immediately to communicate with the environment the app is rendered in. Contentful Management API docs.

Note: The CMA client instance returned by this hook is automatically scoped to the contentful space and environment in which it is called.

Usage:

import { SDKProvider, useCMA } from '@contentful/react-apps-toolkit';

function ComponentUsingCMA() {
  const cma = useCMA();
  const [entries, setEntries] = useState();

  useEffect(() => {
    cma.entry.getMany().then((data) => setEntries(data.items));
  }, [cma]);

  return <>{entries?.length}</>;
}

function App() {
  return (
    <SDKProvider>
      <ComponentUsingCMA />
    </SDKProvider>
  );
}

useFieldValue

useFieldValue provides the current value, and a setter function for updating the current value, of a given field in Contentful. If used in the field location, it will initialize using the current field id by default.

If used in the entry sidebar location, or the entry editor location, it must be passed a field ID to initialize.

useFieldValue also optionally accepts a locale, if the field has multiple locales. If no locale is passed, it will use the environment's default locale.

Usage:

Field component:

import { SDKProvider, useFieldValue } from '@contentful/react-apps-toolkit';

function FieldComponentUsingFieldValue() {
  const [localizedValue, setLocalizedValue] = useFieldValue(sdk.field.id, sdk.field.locale); // specifies the current field and locale

  return <input value={localizedValue} onChange={(e) => setLocalizedValue(e.target.value)} />;
}

function App() {
  return (
    <SDKProvider>
      <FieldComponentUsingFieldValue />
    </SDKProvider>
  );
}

Sidebar or Entry Editor component:

import { SDKProvider, useFieldValue } from '@contentful/react-apps-toolkit';

function ComponentUsingFieldValue() {
  const [defaultValue, setDefaultValue] = useFieldValue('slug', sdk.locales.default); // specifies the "slug" field and the environment's default locale

  return <input value={defaultValue} onChange={(e) => setDefaultValue(e.target.value)} />;
}

function App() {
  return (
    <SDKProvider>
      <ComponentUsingFieldValue />
    </SDKProvider>
  );
}

useAutoResizer

useAutoResizer listens for DOM changes and updates the app's height when the size changes.

Usage:

import { SDKProvider, useAutoResizer } from '@contentful/react-apps-toolkit';

function ComponentUsingAutoResizer() {
  useAutoResizer();

  return <div>Component will be auto-resized</div>;
}

function App() {
  return (
    <SDKProvider>
      <ComponentUsingAutoResizer />
    </SDKProvider>
  );
}

Resources

版本与发布记录

@contentful/react-apps-toolkit 共发布 60 个版本,首次发布于 2022/02/23;过去 12 个月发布了 14 个版本。

版本发布日期距今
3.0.1latest2026/10/2615 天前
3.0.02026/10/2318 天前
2.0.12026/05/225 个月前
2.0.02026/05/145 个月前
1.2.232026/05/135 个月前
1.2.222026/04/087 个月前

其他发布通道:canary → 1.2.23-alpha.9。

关于 @contentful/react-apps-toolkit 的常见问题

@contentful/react-apps-toolkit 是什么?

Toolkit for building a Contentful app in React

如何安装 @contentful/react-apps-toolkit?

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

@contentful/react-apps-toolkit 支持 TypeScript 吗?

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

@contentful/react-apps-toolkit 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@contentful/react-apps-toolkit 可以商用吗?

@contentful/react-apps-toolkit 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@contentful/react-apps-toolkit 的下载量有多少?

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

@contentful/react-apps-toolkit 还在维护吗?

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