@dropinblog/react-core

中文文档
1.1.0发布于 2026/08/06MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7511

Framework agnostic DropInBlog core utilities, router, and React helpers

原始使用文档
文档目录12 节
  1. Features
  2. Installation
  3. Configuration
  4. Routing
  5. Usage
  6. Provider Setup
  7. useDropInBlogRoute — fetch and render a route
  8. useDropInBlogMatch — lightweight route matching
  9. useDropInBlog — access the context directly
  10. Components
  11. Requirements & guarantees
  12. License

@dropinblog/react-core

Framework agnostic React helpers, router utilities, and an API client for the DropInBlog.

Features

  • Router aware – built-in route matching for blog posts, categories, authors, and pagination with customizable base paths.
  • Lightweight API client – fetches rendered content and metadata with automatic caching.
  • Head and body rendering – components for managing SEO tags and rendering blog content.

Installation

npm install @dropinblog/react-core
# or
yarn add @dropinblog/react-core

Configuration

Set your credentials in the environment (recommended):

# For Node.js / SSR environments
DROPINBLOG_BLOG_ID=your_dropinblog_blog_id
DROPINBLOG_API_KEY=your_dropinblog_api_key

# For Vite / browser-only builds, add VITE_ prefix
VITE_DROPINBLOG_BLOG_ID=your_dropinblog_blog_id
VITE_DROPINBLOG_API_KEY=your_dropinblog_api_key

Routing

@dropinblog/react-core ships a mini-router independent of any framework.

Note: If you're already using wouter in your app, use our wouter integration package @dropinblog/react-wouter instead.

Supported route patterns (base path defaults to /blog but is configurable):

  • /blog
  • /blog/page/{page}
  • /blog/category/{slug}
  • /blog/category/{slug}/page/{page}
  • /blog/author/{slug}
  • /blog/author/{slug}/page/{page}
  • /blog/{article-slug}

Usage

Provider Setup

Wrap your app (or blog section) with DropInBlogProvider. When environment variables are set, no props are needed:

import { DropInBlogProvider } from "@dropinblog/react-core";

function App() {
  return (
    <DropInBlogProvider>
      <BlogPage />
    </DropInBlogProvider>
  );
}

You can also pass credentials and options explicitly:

<DropInBlogProvider
  blogId="your_blog_id"
  apiKey="your_api_key"
  basePath="/news"        {/* default: "/blog" */}
  cacheTtlMs={60_000}     {/* default: 5 minutes */}
>
  <BlogPage />
</DropInBlogProvider>

useDropInBlogRoute — fetch and render a route

The primary hook. Pass the current pathname and it matches a route, fetches data, and returns everything you need to render.

import {
  useDropInBlogRoute,
  DropInBlogContent,
  DropInBlogHead,
} from "@dropinblog/react-core";

function BlogPage() {
  const pathname = window.location.pathname; // or useLocation(), etc.
  const { status, resolution, error } = useDropInBlogRoute(pathname);

  if (status === "loading") return <p>Loading…</p>;
  if (status === "error") return <p>Error: {error?.message}</p>;
  if (status !== "success" || !resolution) return null;

  const { body_html, head_data, head_items } = resolution.payload;

  return (
    <>
      <DropInBlogHead headData={head_data} headItems={head_items} />
      <DropInBlogContent bodyHtml={body_html} />
    </>
  );
}

resolution.match tells you which route was matched:

resolution.match.kind;     // "main-list" | "category-list" | "author-list" | "post"
resolution.match.params;   // { slug?: string }
resolution.match.page;     // number (for paginated routes)

useDropInBlogMatch — lightweight route matching

Synchronously checks if a pathname matches a blog route without fetching any data. Useful for conditional rendering or active nav links.

import { useDropInBlogMatch } from "@dropinblog/react-core";

function NavLink() {
  const match = useDropInBlogMatch(window.location.pathname);

  return (
    <a href="/blog" className={match ? "active" : ""}>
      Blog
    </a>
  );
}

Returns a RouteMatch (with kind, params, page) or null if the pathname doesn't match any blog route.

useDropInBlog — access the context directly

Returns the provider context for advanced use cases like calling the API client manually.

import { useDropInBlog } from "@dropinblog/react-core";

function LatestPost() {
  const { client } = useDropInBlog();

  useEffect(() => {
    client.fetchPost("my-post-slug").then((res) => {
      // res.body_html, res.head_data, etc.
    });
  }, []);

  // ...
}

The returned object contains:

  • config — resolved configuration (blogId, basePath, etc.)
  • client — API client with methods like fetchPost(), fetchCategory(), fetchAuthor(), fetchMainList(), and clearCache()
  • router — router instance used for route matching

Components

DropInBlogContent

Renders the blog HTML body.

<DropInBlogContent
  bodyHtml={resolution.payload.body_html}
  as="section"        {/* "article" (default) | "section" | "div" */}
  className="my-blog"
/>

Accepts all standard HTML element props (className, id, style, etc.). Inline scripts in the HTML are automatically executed.

DropInBlogHead

Manages SEO meta tags, JSON-LD schema, stylesheets, and fonts in the document <head>.

<DropInBlogHead
  headData={resolution.payload.head_data}
  headItems={resolution.payload.head_items}
  immediate={true}  {/* default: true — apply to <head> via DOM */}
/>
  • immediate={true} (default) — writes tags directly to document.head and returns null. Best for SPAs.
  • immediate={false} — returns React elements instead, for frameworks that manage <head> themselves.

Requirements & guarantees

  • Node.js ≥ 18 (for built-in fetch).
  • React ≥ 18.2 with forward-compatibility for React 19.
  • No dependencies on Express, Next.js, Remix, React Router, or Wouter.

License

MIT © DropInBlog

版本与发布记录

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

版本发布日期距今
1.1.0latest2026/08/063 个月前
1.0.132026/07/144 个月前
1.0.122026/06/065 个月前
1.0.112026/02/218 个月前
1.0.102025/12/2410 个月前
1.0.92025/12/1011 个月前

关于 @dropinblog/react-core 的常见问题

@dropinblog/react-core 是什么?

Framework agnostic DropInBlog core utilities, router, and React helpers

如何安装 @dropinblog/react-core?

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

@dropinblog/react-core 支持 TypeScript 吗?

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

@dropinblog/react-core 支持 ESM 还是 CommonJS?

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

@dropinblog/react-core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dropinblog/react-core 可以商用吗?

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

@dropinblog/react-core 的下载量有多少?

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

@dropinblog/react-core 还在维护吗?

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