common-hook
Version:
提供项目中常用的 React Hooks
98 lines (54 loc) • 4.58 kB
Markdown
<h1 align="center">common-hook</h1>
<div align="center">
[English](./README.md) | 简体中文
</div>
> 前端业务代码工具库
提供项目中常用的 React Hooks,对输入输出函数做了特殊处理,避免闭包问题,包含丰富的基础 Hooks 以及提炼自业务的高级 Hooks
### 安装说明
```js
npm i common-hook
```
```js
yarn add common-hook
```
### 按需加载
> 默认支持基于 ES Modules 的 Tree Shaking
```js
import { useMount, useUnmount } from "common-hook"
useMount(() => {
console.log("useMount")
})
useUnmount(() => {
console.log("useUnmount")
})
```
## :package: API 文档
### 辅助 Hooks
####   [useLatest](https://github.com/JainaXiong/common-hook/blob/main/src/advanced/useLatest.ts)    返回的永远是最新值
### 浏览器 Hooks
####   [useEventListener](https://github.com/JainaXiong/common-hook/blob/main/src/dom/useEventListener.ts)    事件监听
####   [useTitle](https://github.com/JainaXiong/common-hook/blob/main/src/dom/useTitle.ts)    设置页面标题
####   [useFavicon](https://github.com/JainaXiong/common-hook/blob/main/src/dom/useFavicon.ts)    设置页面 favicon
####   [useHover](https://github.com/JainaXiong/common-hook/blob/main/src/dom/useHover.ts)    监听 DOM 元素是否鼠标悬停
####   [useMouse](https://github.com/JainaXiong/common-hook/blob/main/src/dom/useMouse.ts)    监听鼠标位置
### Effect Hooks
####   [useUpdateEffect](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useUpdateEffect.ts)    首次不执行,只在依赖项更新时执行
####   [useDeepCompareEffect](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useDeepCompareEffect.ts)    依赖项更新时,深度比较执行
####   [useAsyncEffect](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useAsyncEffect.ts)    支持异步函数
####   [useLockFn](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useLockFn.ts)    给一个异步函数增加竞态锁,防止并发执行
####   [useDebounceEffect](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useDebounceEffect.ts)    useEffect+防抖
####   [useDebounceFn](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useDebounceFn.ts)    处理防抖函数的 Hook
####   [useThrottleEffect](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useThrottleEffect.ts)    useEffect+节流
####   [useThrottleFn](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useDebounceFn.ts)    处理函数节流的 Hook
####   [useInterval](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useInterval.ts)    处理 setInterval 的 Hook
####   [useTimeout](https://github.com/JainaXiong/common-hook/blob/main/src/effect/useTimeout.ts)    处理 setTimeout 的 Hook
### 生命周期 Hooks
####   [useMount](https://github.com/JainaXiong/common-hook/blob/main/src/lifeCycle/useMount.ts)    在组件首次渲染时,执行方法
####   [useUnmount](https://github.com/JainaXiong/common-hook/blob/main/src/lifeCycle/useUnmount.ts)    在组件卸载时,执行函数
### 状态 Hooks
####   [useSetState](https://github.com/JainaXiong/common-hook/blob/main/src/state/useSetState.ts)    管理 object 类型 state 的 Hooks
####   [useBoolean](https://github.com/JainaXiong/common-hook/blob/main/src/state/useBoolean.ts)    切换 boolean,可以接收默认值
####   [useToggle](https://github.com/JainaXiong/common-hook/blob/main/src/state/useToggle.ts)    用于在两个状态值间切换 Hook
####   [useDebounce](https://github.com/JainaXiong/common-hook/blob/main/src/state/useDebounce.ts)    处理防抖值 Hook
####   [useThrottle](https://github.com/JainaXiong/common-hook/blob/main/src/state/useThrottle.ts)    处理节流值 Hook
####   [useRafState](https://github.com/JainaXiong/common-hook/blob/main/src/state/useRafState.ts)    只在 requestAnimationFrame callback 时更新 state