@ctrl/react-adsense

中文文档
3.0.0发布于 2026/10/27MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2万

React component for Google AdSense

原始使用文档
文档目录5 节
  1. Install
  2. Use
  3. Props
  4. Server rendering
  5. Single page apps

@ctrl/react-adsense npm

React component for Google AdSense

DEMO: https://react-adsense.xmplaylist.com

Install

npm install @ctrl/react-adsense

Requires React 19.3 or newer.

Use

Load the AdSense script once, either in your HTML <head> as you normally would or anywhere in your React tree. React hoists async scripts into the <head> and only loads them once.

<script
  async
  src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
  crossOrigin="anonymous"
/>
import { Adsense } from '@ctrl/react-adsense';

// ads with no set-up
<Adsense
  client="ca-pub-7640562161899788"
  slot="7259870550"
/>

// ads with a fixed size
<Adsense
  client="ca-pub-7640562161899788"
  slot="7259870550"
  style={{ display: 'inline-block', width: 300, height: 250 }}
  format=""
/>

// responsive and native ads
<Adsense
  client="ca-pub-7640562161899788"
  slot="7259870550"
  style={{ textAlign: 'center' }}
  layout="in-article"
  format="fluid"
/>

Props

PropSetsDefault
clientdata-ad-client
slotdata-ad-slot
layoutdata-ad-layout
layoutKeydata-ad-layout-key
formatdata-ad-format'auto'
responsivedata-full-width-responsive'false'
adTestdata-adtest
pageLevelAdspushes enable_page_level_ads for clientfalse

style is merged over { display: 'block' }. Everything else (className, id, ref, data-*, ...) is passed to the <ins>.

Server rendering

Works with server rendering (Next.js, TanStack Start, React Router, ...) and React Server Components, no wrapper needed. The package is marked 'use client', and the ad itself only renders in the browser using React's browser().

  • The server sends an empty <ins> with your className and style. Give it a size so the page doesn't shift when the ad loads, like style={{ minHeight: 280 }} for a responsive ad.
  • After hydration the browser swaps in the real <ins class="adsbygoogle"> and requests the ad. AdSense can't fill a slot React hasn't hydrated yet, which used to cause hydration errors.
  • Server renderers report this through onBrowserBailout, not onError, so it doesn't show up as an error.
// app/page.tsx, a server component
import { Adsense } from '@ctrl/react-adsense';

export default function Page() {
  return <Adsense client="ca-pub-7640562161899788" slot="7259870550" style={{ minHeight: 280 }} />;
}

Single page apps

Each <Adsense> asks AdSense for one ad when it mounts, changing its props doesn't load another. Give it a key that changes when you want a new ad, like the current route.

<Adsense key={pathname} client="ca-pub-7640562161899788" slot="7259870550" />

版本与发布记录

@ctrl/react-adsense 共发布 24 个版本,首次发布于 2020/06/08;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/2715 天前
2.1.02025/09/161.1 年前
2.0.22025/09/161.2 年前
2.0.12025/09/161.2 年前
2.0.02025/08/091.3 年前
1.8.02024/12/061.9 年前

关于 @ctrl/react-adsense 的常见问题

@ctrl/react-adsense 是什么?

React component for Google AdSense

如何安装 @ctrl/react-adsense?

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

@ctrl/react-adsense 支持 TypeScript 吗?

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

@ctrl/react-adsense 支持 ESM 还是 CommonJS?

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

@ctrl/react-adsense 可以商用吗?

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

@ctrl/react-adsense 的下载量有多少?

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

@ctrl/react-adsense 还在维护吗?

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