@gadgetinc/client-hooks

中文文档
0.1.7发布于 2026/02/10MITTypeScriptESM + CJS近 30 天下载 3.1万

This package contains shared implementations of core hooks used in React/Preact libraries for Gadget applications. @gadgetinc/client-hooks provides framework-agnostic hook implementations that are consumed by framework-specific packages like `@gadgetinc

原始使用文档
文档目录8 节
  1. Overview
  2. Architecture
  3. Available Hooks
  4. Query Hooks
  5. Mutation Hooks
  6. Utility Hooks
  7. How Framework Packages Use This
  8. For Gadget Developers

Client Hooks

This package contains shared implementations of core hooks used in React/Preact libraries for Gadget applications. @gadgetinc/client-hooks provides framework-agnostic hook implementations that are consumed by framework-specific packages like @gadgetinc/react and @gadgetinc/preact.

Note that developers shouldn't need to use this package directly and should instead use their generated client plus framework-specific binding packages like @gadgetinc/react.

Overview

@gadgetinc/client-hooks is designed with a runtime adapter pattern that allows the same hook logic to work across different UI frameworks (React, Preact, etc.). The package exports:

  • Core hook implementations for querying and mutating Gadget APIs
  • A RuntimeAdapter interface that framework-specific packages implement
  • A registration system via registerClientHooks to bind hooks to a specific framework

Architecture

The library uses an adapter pattern to remain framework-agnostic:

interface RuntimeAdapter {
  GadgetApiContext: Context<GadgetApiContext>;
  framework: FrameworkBindings; // useState, useEffect, useMemo, etc.
  urql: UrqlBindings; // useQuery, useMutation
}

Framework-specific packages (like @gadgetinc/react) provide an adapter implementation that maps to their framework's primitives, then call registerClientHooks to initialize the hooks with that adapter.

Available Hooks

Query Hooks

  • useFindOne - Fetch a single record by ID
  • useMaybeFindOne - Fetch a single record by ID, returning null if not found
  • useFindMany - Fetch a page of records with filtering, sorting, and pagination
  • useFindFirst - Fetch the first record matching criteria
  • useMaybeFindFirst - Fetch the first record matching criteria, returning null if not found
  • useFindBy - Fetch a record by a unique field value
  • useGet - Fetch a singleton record (e.g., current session)
  • useView - Fetch records from a backend view

Mutation Hooks

  • useAction - Run an action on a model record (create, update, delete, etc.)
  • useBulkAction - Run an action on multiple records at once
  • useGlobalAction - Run a global action

Utility Hooks

  • useFetch - Make HTTP requests to backend routes
  • useEnqueue - Enqueue background actions
  • useQuery - Low-level GraphQL query hook
  • useMutation - Low-level GraphQL mutation hook
  • useApi - Access the Gadget API client instance
  • useConnection - Access the Gadget connection instance
  • useCoreImplementation - Access the core implementation details

How Framework Packages Use This

Framework-specific packages like @gadgetinc/react follow this pattern:

  1. Import hooks and utilities from @gadgetinc/client-hooks
  2. Create a RuntimeAdapter implementation for their framework
  3. Create a Provider component that calls registerClientHooks
  4. Re-export the hooks for end users

Example:

import { registerClientHooks, useAction, useFindMany } from "@gadgetinc/client-hooks";
import { useContext, useMemo /* ... */ } from "react";
import { useQuery, useMutation } from "urql";

// Create adapter for React
const adapter: RuntimeAdapter = {
  framework: { useContext, useMemo /* ... */ },
  urql: { useQuery, useMutation, Provider },
  GadgetApiContext: createContext(/* ... */),
};

// Provider component initializes the hooks
export const Provider = ({ api, children }) => {
  const { gadgetClient, urqlClient } = registerClientHooks(api, adapter);
  // ... render provider with context
};

// Re-export hooks for users
export { useAction, useFindMany /* ... */ };

For Gadget Developers

This package is designed to reduce code duplication between React and Preact (and potentially other frameworks). When adding new functionality:

  1. Implement the hook logic once in @gadgetinc/client-hooks
  2. Use the RuntimeAdapter for any framework-specific calls
  3. Export the hook from the package
  4. Re-export from framework packages like @gadgetinc/react

The hook registration system uses a stub pattern where hooks throw helpful errors if called outside a proper Provider context, then get replaced with real implementations once registerClientHooks is called.

版本与发布记录

@gadgetinc/client-hooks 共发布 8 个版本,首次发布于 2025/10/30;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.1.7latest2026/02/109 个月前
0.1.62026/01/0310 个月前
0.1.52025/12/0411 个月前
0.1.42025/11/1711 个月前
0.1.32025/11/131 年前
0.1.22025/10/301 年前

关于 @gadgetinc/client-hooks 的常见问题

@gadgetinc/client-hooks 是什么?

This package contains shared implementations of core hooks used in React/Preact libraries for Gadget applications. @gadgetinc/client-hooks provides framework-agnostic hook implementations that are consumed by framework-specific packages like `@gadgetinc

如何安装 @gadgetinc/client-hooks?

在项目目录运行 npm install @gadgetinc/client-hooks,或使用 pnpm add / yarn add / bun add。

@gadgetinc/client-hooks 支持 TypeScript 吗?

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

@gadgetinc/client-hooks 支持 ESM 还是 CommonJS?

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

@gadgetinc/client-hooks 可以商用吗?

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

@gadgetinc/client-hooks 的下载量有多少?

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

@gadgetinc/client-hooks 还在维护吗?

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