@adhese/sdk-react

中文文档
1.13.2发布于 2026/11/05GPL-3.0TypeScriptESM + CJS构建来源已验证近 30 天下载 5093

Adhese SDK

原始使用文档
文档目录5 节
  1. Installation
  2. AdheseProvider
  3. useAdhese
  4. useAdheseSlot
  5. useAdheseSlot with onBeforeRender

Adhese React SDK

For React developers, we provide a React SDK that allows you to easily integrate our services into your React application.

The React SDK is built on top of the Adhese JavaScript SDK and doesn't need the Adhese JavaScript SDK to be installed separately.

Installation

npm install @adhese/sdk-react

AdheseProvider

The AdheseProvider component is a context provider that makes the Adhese instance available to all child components. It should be placed at the root of your application. It accepts an options prop that is passed to the createAdhese function. When the options prop changes, the Adhese instance is recreated. This provider replaces the createAdhese function.

The AdheseProvider component is a wrapper around the createAdhese function. It is required to use the AdheseProvider if you want to use the useAdhese or useAdheseSlot hooks.

import { AdheseProvider } from '@adhese/sdk-react';

function Devtools() {
  return (
    <AdheseProvider options={{
      account: 'your-account-id',
    }}>
      <YourApp />
    </AdheseProvider>
  );
}

useAdhese

The useAdhese hook returns the Adhese instance that is created by the AdheseProvider. It can be used to access the Adhese instance in any child component.

import { useAdhese } from '@adhese/sdk-react';

function YourComponent() {
  const adhese = useAdhese();
  // Use the adhese instance
}

useAdheseSlot

The useAdheseSlot hook returns a slot object for a given slot name. Use the to create a slot in your component.

It accepts the following arguments:

  • elementRef: A ref to the element that the slot should be attached to.
  • options: An object with options for the slot. This object is passed to the adhese.addSlot function.

When your component is unmounted, the slot is automatically removed from the Adhese instance.

import { useAdheseSlot } from '@adhese/sdk-react';

function YourComponent() {
  const elementRef = useRef(null);

  const slot = useAdheseSlot(elementRef, {
    format: 'your-format',
  });

  return (
    <div ref={elementRef} />
  );
}

useAdheseSlot with onBeforeRender

Like described in the slots documentation, you can use the onBeforeRender callback to intercept the to be rendered ad. The example there is written in vanilla JavaScript. To usejsx you can use the React renderToStaticMarkup function to create static HTML while still having the benefits of JSX.

import { renderToStaticMarkup } from 'react-dom/server';
import { useAdheseSlot } from '@adhese/sdk-react';

function YourComponent() {
  const adhese = useAdhese();
  const elementRef = useRef(null);

  const slot = useAdheseSlot(elementRef, {
    format: 'your-format',
    onBeforeRender: (ad) => {
      if (typeof ad.tag !== 'object') {
        // If the tag is not an object, return the ad as is
        return ad;
      }

      return {
        ...ad,
        tag: renderToStaticMarkup((
          <div>
            <h1>{ad.tag.title}</h1>
            <p>{ad.tag.description}</p>
          </div>
        )),
      }
    },
  });

  return (
    <div ref={slot.elementRef} />
  );
}

版本与发布记录

@adhese/sdk-react 共发布 257 个版本,首次发布于 2024/03/22;过去 12 个月发布了 73 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.13.2latest2026/11/05今天
1.13.2-nightly-20261105093040nightly2026/11/051 天前
1.13.12026/10/2710 天前
1.13.1-nightly-202610270804472026/10/2710 天前
1.13.1-nightly-202610270758582026/10/2710 天前
1.13.02026/10/2115 天前

关于 @adhese/sdk-react 的常见问题

@adhese/sdk-react 是什么?

Adhese SDK

如何安装 @adhese/sdk-react?

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

@adhese/sdk-react 支持 TypeScript 吗?

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

@adhese/sdk-react 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@adhese/sdk-react 可以商用吗?

@adhese/sdk-react 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@adhese/sdk-react 的下载量有多少?

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

@adhese/sdk-react 还在维护吗?

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