react-use-hooks
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact-Use Hooks
React-Use Hooks
This skill is a decision-and-implementation guide for react-use hooks in React projects. It maps requirements to the most suitable react-use hook, applies the correct usage pattern, and prefers hook-based solutions over bespoke code to keep implementations concise, maintainable, and performant.
本指南是React项目中react-use hooks的决策与实现参考,它将需求映射到最合适的react-use hook,采用正确的使用模式,优先选择基于hook的解决方案而非自定义代码,以保持实现的简洁性、可维护性和性能。
When to Apply
适用场景
- Apply this skill whenever assisting user development work in React.
- Always check first whether a react-use hook can implement the requirement.
- Prefer react-use hooks over custom code to improve readability, maintainability, and performance.
- Map requirements to the most appropriate hook and follow the hook's invocation rule.
- Please refer to the field in the below hooks table. For example:
Invocation- : Use automatically when applicable.
AUTO - : Use only if the user already installed the required external dependency; otherwise reconsider, and ask to install only if truly needed.
EXTERNAL - : Use only when explicitly requested by the user.
EXPLICIT_ONLY
NOTE User instructions in the prompt ormay override a hook's defaultAGENTS.mdrule.Invocation
- 每当协助用户进行React开发工作时,都可以运用本指南。
- 首先始终要检查是否可以通过react-use hook来实现需求。
- 优先使用react-use hooks而非自定义代码,以提升可读性、可维护性和性能。
- 将需求映射到最合适的hook,并遵循该hook的调用规则。
- 请参考下方hooks表格中的字段。例如:
Invocation- : 适用时自动使用。
AUTO - : 仅当用户已安装所需的外部依赖时使用;否则重新考虑,仅在确实需要时才要求用户安装。
EXTERNAL - : 仅在用户明确要求时使用。
EXPLICIT_ONLY
注意:提示中的用户指令或可能会覆盖hook的默认AGENTS.md规则。Invocation
Hooks
Hooks
All hooks listed below are part of the react-use library, each section categorizes hooks based on their functionality.
IMPORTANT: Each hook entry includes a short and a detailed . When using any hook, always consult the corresponding document in for Usage details and Type Declarations.
DescriptionReference./references以下列出的所有hooks均属于react-use库,各章节根据功能对hooks进行分类。
重要提示:每个hook条目都包含简短的和详细的。使用任何hook时,请务必查阅中的对应文档,了解使用细节和类型声明。
DescriptionReference./referencesSensors
传感器类
| Hook | Description | Invocation |
|---|---|---|
| laptopL: 1440, laptop: 1024, tablet: 768 | AUTO |
| React sensor hook that tracks battery status. | AUTO |
| React sensor hook that tracks user's geographic location. This hook accepts [position options](https | AUTO |
| React sensor hook that tracks browser's location hash. | AUTO |
| React UI sensor hooks that track if some element is being hovered | AUTO |
| Tracks mouse hover state using a ref (more direct than useHover). | AUTO |
| React sensor hook that tracks if user on the page is idle. | AUTO |
| React sensor hook that tracks the changes in the intersection of a target element with an ancestor e | AUTO |
| React UI sensor hook that executes a | AUTO |
| React UI sensor hook that detects complex key combos like detecting when | EXTERNAL |
| React UI sensor hook that detects when the user is pressing a specific | AUTO |
| This hook fires | AUTO |
| React sensor hook that tracks brower's location. | AUTO |
| React sensor hook that fires a callback after long pressing. | AUTO |
| React sensor hook that tracks dimensions of an HTML element using the [Resize Observer API](https:// | AUTO |
| React sensor hook that tracks state of a CSS media query. | AUTO |
| React sensor hook that tracks connected hardware devices. | AUTO |
| React sensor hook that uses device's acceleration sensor to track its motions. | AUTO |
| React sensor hooks that re-render on mouse position changes. | AUTO |
| Extended mouse tracking with options for bounded coordinates and hover-only tracking. | AUTO |
| React Hook to get deltaY of mouse scrolled in window. | AUTO |
| Tracks the state of browser's network connection. | AUTO |
| React sensor hook that tracks screen orientation of user's device. | AUTO |
| React sensor hook that fires a callback when mouse leaves the page. | AUTO |
| React sensor hook that tracks the changes in pointer touch events and detects value of pinch differe | AUTO |
| React sensor hook that tracks state of mouse "scrubs" (or "scratches"). | AUTO |
| React sensor hook that re-renders when the scroll position in a DOM element changes. | AUTO |
| Hook that will return current browser's scrollbar width. | AUTO |
| React sensor hook that keeps track of whether the user is scrolling or not. | AUTO |
| React sensor hook that tracks browser's location search param. | AUTO |
| React sensor hook that tracks size of an HTML element. | AUTO |
| React sensor hook that fires a callback when user starts typing. Can be used | AUTO |
| React sensor hook that re-renders on window scroll. | AUTO |
| React sensor hook that tracks dimensions of the browser window. | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| laptopL: 1440, laptop: 1024, tablet: 768 | AUTO |
| 跟踪电池状态的React传感器hook。 | AUTO |
| 跟踪用户地理位置的React传感器hook。该hook接受[定位选项](https | AUTO |
| 跟踪浏览器地址栏哈希值的React传感器hook。 | AUTO |
| 跟踪元素是否被悬停的React UI传感器hook | AUTO |
| 使用ref跟踪鼠标悬停状态(比useHover更直接)。 | AUTO |
| 跟踪页面用户是否处于空闲状态的React传感器hook。 | AUTO |
| 跟踪目标元素与祖先元素交叉变化的React传感器hook | AUTO |
| 当按下键盘按键时执行 | AUTO |
| 检测复杂组合键的React UI传感器hook,比如检测当 | EXTERNAL |
| 检测用户是否正在按下特定按键的React UI传感器hook | AUTO |
| 该hook触发 | AUTO |
| 跟踪浏览器地址的React传感器hook。 | AUTO |
| 长按后触发回调的React传感器hook。 | AUTO |
| 使用[Resize Observer API](https://跟踪HTML元素尺寸的React传感器hook。 | AUTO |
| 跟踪CSS媒体查询状态的React传感器hook。 | AUTO |
| 跟踪已连接硬件设备的React传感器hook。 | AUTO |
| 使用设备加速度传感器跟踪运动状态的React传感器hook。 | AUTO |
| 鼠标位置变化时重新渲染的React传感器hook。 | AUTO |
| 扩展鼠标跟踪,支持边界坐标和仅悬停跟踪选项。 | AUTO |
| 获取窗口中鼠标滚动deltaY值的React Hook。 | AUTO |
| 跟踪浏览器网络连接状态。 | AUTO |
| 跟踪用户设备屏幕方向的React传感器hook。 | AUTO |
| 当鼠标离开页面时触发回调的React传感器hook。 | AUTO |
| 跟踪指针触摸事件变化并检测捏合缩放值的React传感器hook | AUTO |
| 跟踪鼠标“刮擦”状态的React传感器hook。 | AUTO |
| DOM元素滚动位置变化时重新渲染的React传感器hook。 | AUTO |
| 返回当前浏览器滚动条宽度的hook。 | AUTO |
| 跟踪用户是否正在滚动的React传感器hook。 | AUTO |
| 跟踪浏览器地址栏查询参数的React传感器hook。 | AUTO |
| 跟踪HTML元素尺寸的React传感器hook。 | AUTO |
| 用户开始输入时触发回调的React传感器hook,可用于 | AUTO |
| 窗口滚动时重新渲染的React传感器hook。 | AUTO |
| 跟踪浏览器窗口尺寸的React传感器hook。 | AUTO |
UI
UI类
| Hook | Description | Invocation |
|---|---|---|
| Creates | AUTO |
| React UI hook that triggers a callback when user | AUTO |
| React UI hook that changes [CSS dynamically][gen-5]. Works like "virtual CSS" — | AUTO |
| Triggers on file, link drop and copy-paste. | AUTO |
| Display an element full-screen, optional fallback for fullscreen video on iOS. | AUTO |
| React UI hook that provides slide behavior over any HTML element. Supports both mouse and touch even | AUTO |
| React UI hook that synthesizes human voice that speaks a given string. | AUTO |
| React UI hook to provide physical feedback with device vibration hardware using the [Vibration API]( | AUTO |
| Creates | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| 创建 | AUTO |
| 用户点击元素外部时触发回调的React UI hook | AUTO |
| 动态修改[CSS][gen-5]的React UI hook。类似于“虚拟CSS”—— | AUTO |
| 触发文件、链接拖放和粘贴事件。 | AUTO |
| 将元素显示为全屏,支持iOS上全屏视频的备选方案。 | AUTO |
| 为任意HTML元素提供滑动行为的React UI hook,支持鼠标和触摸事件 | AUTO |
| 将给定字符串合成为人声的React UI hook。 | AUTO |
| 使用[Vibration API]实现设备振动物理反馈的React UI hook | AUTO |
| 创建 | AUTO |
Animations
动画类
| Hook | Description | Invocation |
|---|---|---|
| Same as | AUTO |
| A declarative interval hook based on [Dan Abramov's article on overreacted.io](https://overreacted.i | AUTO |
| React animation hook that forces component to re-render on each | AUTO |
| React animation hook that updates a single numeric value over time according | EXTERNAL |
| Re-renders the component after a specified number of milliseconds. | AUTO |
| Calls given function after specified amount of milliseconds. | AUTO |
| React animation hook that tweens a number between 0 and 1. | AUTO |
| React utility hook that returns a function that forces component | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| 与 | AUTO |
| 基于Dan Abramov在overreacted.io上的文章实现的声明式interval hook | AUTO |
| 强制组件在每个 | AUTO |
| 随时间更新单个数值的React动画hook,遵循 | EXTERNAL |
| 指定毫秒数后重新渲染组件。 | AUTO |
| 指定毫秒数后调用给定函数。 | AUTO |
| 在0到1之间补间数值的React动画hook。 | AUTO |
| 返回强制组件重新渲染函数的React工具hook | AUTO |
Side-Effects
副作用类
| Hook | Description | Invocation |
|---|---|---|
| React hook that resolves an | AUTO |
| React hook that returns state and a callback for an | AUTO |
| Uses | AUTO |
| React side-effect hook that shows browser alert when user try to reload or close the page. | AUTO |
| React hook that returns the current value of a | AUTO |
| Copy text to a user's clipboard. | AUTO |
| React hook that delays invoking a function until after wait milliseconds have elapsed since the last | AUTO |
| React side-effect hook that returns an error dispatcher. | AUTO |
| React side-effect hook sets the favicon of the page. | AUTO |
| React side-effect hook that manages a single | AUTO |
| React side-effect hook that locks scrolling on the body element. Useful for modal and other overlay | AUTO |
| React side-effect hook to query permission status of browser APIs. | AUTO |
| This hook call given function within the RAF loop without re-rendering parent component. | AUTO |
| React side-effect hook that manages a single | AUTO |
| React hooks that throttle. | AUTO |
| React hook that invokes a function and then delays subsequent function calls until after wait millis | AUTO |
| React side-effect hook that sets title of the page. | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| 解析 | AUTO |
| 返回状态和 | AUTO |
| 基于 | AUTO |
| 用户尝试刷新或关闭页面时显示浏览器提示的React副作用hook。 | AUTO |
| 返回 | AUTO |
| 将文本复制到用户剪贴板。 | AUTO |
| 延迟调用函数,直到距离上次调用已过指定毫秒数的React hook | AUTO |
| 返回错误调度器的React副作用hook。 | AUTO |
| 设置页面图标的React副作用hook。 | AUTO |
| 管理单个 | AUTO |
| 锁定body元素滚动的React副作用hook,适用于模态框和其他覆盖层 | AUTO |
| 查询浏览器API权限状态的React副作用hook。 | AUTO |
| 在RAF循环中调用给定函数而不重新渲染父组件的hook。 | AUTO |
| 管理单个 | AUTO |
| 实现节流功能的React hook。 | AUTO |
| 调用函数后延迟后续调用,直到距离上次调用已过指定毫秒数的React hook | AUTO |
| 设置页面标题的React副作用hook。 | AUTO |
Lifecycles
生命周期类
| Hook | Description | Invocation |
|---|---|---|
| A modified useEffect hook that accepts a comparator which is used for comparison on dependencies ins | AUTO |
| A modified useEffect hook that is using deep comparison on its dependencies instead of reference equ | AUTO |
| React lifecycle hook that runs an effect only once. | AUTO |
| React sensor hook that subscribes a | AUTO |
| | AUTO |
| React lifecycle hook that call | AUTO |
| React lifecycle hook that console logs parameters as component transitions through lifecycles. | AUTO |
| React lifecycle hook that calls a function after the component is mounted. Use | AUTO |
| > NOTE!: despite having | AUTO |
| React Lifecycle hook that returns a helper function for wrapping promises. | AUTO |
| A modified useEffect hook that is using shallow comparison on each of its dependencies instead of re | AUTO |
| React lifecycle hook that calls a function when the component will unmount. Use | AUTO |
| A life-cycle hook that provides a higher order promise that does not resolve if component un-mounts. | AUTO |
| React effect hook that ignores the first invocation (e.g. on mount). The signature is exactly the sa | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| 改良版useEffect hook,接受比较器用于依赖项比较 | AUTO |
| 改良版useEffect hook,对依赖项进行深度比较而非引用比较 | AUTO |
| 仅运行一次effect的React生命周期hook。 | AUTO |
| 订阅 | AUTO |
| 服务端渲染时不显示警告的 | AUTO |
| 组件挂载和卸载时调用 | AUTO |
| 组件生命周期转换时在控制台记录参数的React生命周期hook。 | AUTO |
| 组件挂载后调用函数的React生命周期hook。如果需要同时处理挂载和卸载,请使用 | AUTO |
| > 注意!: 尽管名称中包含 | AUTO |
| 返回包装Promise辅助函数的React生命周期hook。 | AUTO |
| 改良版useEffect hook,对每个依赖项进行浅比较而非引用比较 | AUTO |
| 组件即将卸载时调用函数的React生命周期hook。如果需要同时处理挂载和卸载,请使用 | AUTO |
| 提供高阶Promise的生命周期hook,组件卸载时Promise不会 resolve。 | AUTO |
| 忽略首次调用(如挂载时)的React effect hook,签名与标准useEffect完全相同 | AUTO |
State
状态管理类
| Hook | Description | Invocation |
|---|---|---|
| A React hook that creates a globally shared state. | AUTO |
| Hook factory, receives a function to be memoized, returns a memoized React hook, | AUTO |
| Factory for reducer hooks with custom middleware with an identical API as [React's | AUTO |
| Factory for react context hooks that will behave just like [React's | AUTO |
| Factory for react context hooks that will behave just like [React's | AUTO |
| Alias for | AUTO |
| React state hook that tracks a numeric value. | AUTO |
| React state hook that returns the default value when state is null or undefined. | AUTO |
| Returns | AUTO |
| React state hook that returns state getter function instead of | AUTO |
| A mix of | AUTO |
| React state hook that returns the latest state as described in the [React hooks FAQ](https://reactjs | AUTO |
| Tracks an array and provides methods to modify it. | AUTO |
| React state hook that tracks a value of an object. | AUTO |
| A lot like the standard | AUTO |
| React hook that simplifies the | AUTO |
| Each time any of given states changes - validator function is invoked. | AUTO |
| Alias for | AUTO |
| React state hook that tracks the latest value of an | EXTERNAL |
| React state hook that returns the previous state as described in the [React hooks FAQ](https://react | AUTO |
| Just like | AUTO |
| React state hook implements simple FIFO queue. | AUTO |
| React state hook that only updates state in the callback of [ | AUTO |
| Tracks component's renders count including the first render. | AUTO |
| React state hook that tracks a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen | AUTO |
| React state hook that creates | AUTO |
| Provides handles for circular iteration over states list. | AUTO |
| Each time given state changes - validator function is invoked. | AUTO |
| Stores defined amount of previous state values and provides handles to travel through them. | AUTO |
| React state hook that tracks value of a boolean. | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| 创建全局共享状态的React hook。 | AUTO |
| Hook工厂,接收待 memoize 的函数,返回memoized的React hook | AUTO |
| 带自定义中间件的reducer hook工厂,API与React的 | AUTO |
| React上下文hook工厂,行为与React的 | AUTO |
| React上下文hook工厂,行为与React的 | AUTO |
| | AUTO |
| 跟踪数值的React状态hook。 | AUTO |
| 状态为null或undefined时返回默认值的React状态hook。 | AUTO |
| 组件刚挂载(首次渲染)时返回 | AUTO |
| 返回状态获取函数而非直接返回状态的React状态hook | AUTO |
| | AUTO |
| 返回最新状态的React状态hook,详见[React hooks FAQ](https://reactjs | AUTO |
| 跟踪数组并提供修改方法。 | AUTO |
| 跟踪对象值的React状态hook。 | AUTO |
| 与标准 | AUTO |
| 简化 | AUTO |
| 任意给定状态变化时,调用验证函数。 | AUTO |
| | AUTO |
| 跟踪 | EXTERNAL |
| 返回上一次状态的React状态hook,详见[React hooks FAQ](https://react | AUTO |
| 与 | AUTO |
| 实现简单FIFO队列的React状态hook。 | AUTO |
| 仅在 | AUTO |
| 跟踪组件渲染次数(包括首次渲染)。 | AUTO |
| 跟踪[Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referen的React状态hook | AUTO |
| 创建类似类组件 | AUTO |
| 提供状态列表循环迭代的处理方法。 | AUTO |
| 给定状态变化时,调用验证函数。 | AUTO |
| 存储指定数量的历史状态值,并提供回溯方法。 | AUTO |
| 跟踪布尔值的React状态hook。 | AUTO |
Miscellaneous
其他类
| Hook | Description | Invocation |
|---|---|---|
| React hook to use a ForwardedRef safely. | AUTO |
| Hook | Description | Invocation |
|---|---|---|
| 安全使用ForwardedRef的React hook。 | AUTO |