hooks
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCustom Hook Guidelines
自定义 Hook 规范
This rule outlines the guidelines for creating custom React hooks within this project.
本规则概述了在本项目中创建自定义React Hooks的相关指南。
Custom Hooks
自定义 Hooks
- Purpose: Encapsulate reusable stateful logic, especially for data fetching or complex UI interactions.
- Location: Place custom hooks in the directory.
apps/web/hooks/ - Naming: Use the prefix (e.g.,
use).useAccounts.ts - Data Fetching: For fetching data from API endpoints, prefer using . Follow the guidelines outlined in data-fetching.mdc.
useSWR- Create dedicated hooks for specific data types (e.g., ,
useAccounts).useLabels - The hook should typically wrap , handle the API endpoint URL, and return the data, loading state, error state, and potentially the
useSWRfunction from SWR.mutate
- Create dedicated hooks for specific data types (e.g.,
- Simplicity: Keep hooks focused on a single responsibility.
By adhering to these guidelines, we ensure a consistent approach to reusable logic and data fetching throughout the application.
- 用途: 封装可复用的有状态逻辑,尤其适用于数据获取或复杂UI交互场景。
- 存放位置: 将自定义Hook放置在目录下。
apps/web/hooks/ - 命名规则: 使用作为前缀(例如:
use)。useAccounts.ts - 数据获取: 从API端点获取数据时,优先使用。请遵循data-fetching.mdc中概述的指南。
useSWR- 为特定数据类型创建专用Hook(例如:、
useAccounts)。useLabels - Hook通常应封装,处理API端点URL,并返回数据、加载状态、错误状态,以及可能的SWR中的
useSWR函数。mutate
- 为特定数据类型创建专用Hook(例如:
- 简洁性: 保持Hook专注于单一职责。
遵循这些指南,我们可确保在整个应用中采用一致的方式处理可复用逻辑和数据获取。