-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(uselatest & useunmount): remove deps ahooks, add useLatest & use…
…Unmount
- Loading branch information
Showing
14 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default { | ||
navigationBarTitleText: 'useLatest', | ||
enableShareAppMessage: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* title: 基础用法 | ||
* desc: useLatest 返回的永远是最新值 | ||
*/ | ||
import React, { useState, useEffect } from 'react'; | ||
import { View } from '@tarojs/components'; | ||
|
||
import DocPage from '@components/DocPage'; | ||
import { useLatest } from 'taro-hooks'; | ||
|
||
export default () => { | ||
const [count, setCount] = useState(0); | ||
|
||
const latestCountRef = useLatest(count); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setCount(latestCountRef.current + 1); | ||
}, 1000); | ||
return () => clearInterval(interval); | ||
}, []); | ||
|
||
return ( | ||
<DocPage title="useLatest 使用最新值" panelTitle="useLatest"> | ||
<View>count: {count}</View> | ||
</DocPage> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default { | ||
navigationBarTitleText: 'useUnmount', | ||
enableShareAppMessage: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* title: 基础用法 | ||
* desc: 在组件卸载时,执行函数。 | ||
*/ | ||
import React, { useState } from 'react'; | ||
import { View } from '@tarojs/components'; | ||
import { AtMessage, AtButton } from 'taro-ui'; | ||
import Taro from '@tarojs/taro'; | ||
|
||
import DocPage from '@components/DocPage'; | ||
import { useUnmount } from 'taro-hooks'; | ||
|
||
const MyComponent = () => { | ||
useUnmount(() => { | ||
Taro.atMessage({ | ||
type: 'info', | ||
message: 'unmount', | ||
}); | ||
}); | ||
|
||
return <View className="gap">Hello World!</View>; | ||
}; | ||
|
||
export default () => { | ||
const [state, toggle] = useState(true); | ||
|
||
return ( | ||
<DocPage title="useUnmount 组件卸载时执行" panelTitle="useUnmount"> | ||
<AtMessage /> | ||
<AtButton type="primary" onClick={() => toggle(!state)}> | ||
{state ? 'unmount' : 'mount'} | ||
</AtButton> | ||
{state && <MyComponent />} | ||
</DocPage> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: useLatest | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 基础 | ||
path: /basic | ||
--- | ||
|
||
<Alert>注意: 该 Hook 是来自<a target="__blank" href="https://ahooks.js.org/zh-CN/hooks/use-latest">ahook useLatest</a>. 主要是为了分离主包</Alert> | ||
|
||
# useLatest | ||
|
||
返回当前最新值的 Hook,可以避免闭包问题。 | ||
|
||
## 何时使用 | ||
|
||
当需获取最新值时。 | ||
|
||
## API | ||
|
||
```typescript | ||
const latestValueRef = useLatest<T>(value: T): MutableRefObject<T>; | ||
``` | ||
|
||
### Params | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ----- | -------- | ---- | ------ | | ||
| value | 获取的值 | `T` | - | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useLatest" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | ✔️ | ✔️ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useRef } from 'react'; | ||
|
||
function useLatest<T>(value: T) { | ||
const ref = useRef(value); | ||
ref.current = value; | ||
|
||
return ref; | ||
} | ||
|
||
export default useLatest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: useUnmount | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 基础 | ||
path: /basic | ||
--- | ||
|
||
<Alert>注意: 该 Hook 是来自<a target="__blank" href="https://ahooks.js.org/zh-CN/hooks/use-unmount">ahook useUnmount</a>. 主要是为了分离主包</Alert> | ||
|
||
# useUnmount | ||
|
||
在组件卸载(unmount)时执行的 Hook。 | ||
|
||
## 何时使用 | ||
|
||
当需监听组件卸载时 | ||
|
||
## API | ||
|
||
```typescript | ||
useUnmount(fn: () => void); | ||
``` | ||
|
||
### Params | ||
|
||
| 参数 | 说明 | 类型 | 默认值 | | ||
| ---- | -------------------- | ------------ | ------ | | ||
| fn | 组件卸载时执行的函数 | `() => void` | - | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useUnmount" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | ✔️ | ✔️ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect } from 'react'; | ||
import useLatest from '../useLatest'; | ||
|
||
const useUnmount = (fn: () => void) => { | ||
if (process.env.NODE_ENV === 'development') { | ||
if (typeof fn !== 'function') { | ||
console.error( | ||
`useUnmount expected parameter is a function, got ${typeof fn}`, | ||
); | ||
} | ||
} | ||
|
||
const fnRef = useLatest(fn); | ||
|
||
useEffect( | ||
() => () => { | ||
fnRef.current(); | ||
}, | ||
[], | ||
); | ||
}; | ||
|
||
export default useUnmount; |
32708ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
taro-hooks – ./
taro-hooks-git-main-innocces.vercel.app
taro-hooks-theta.vercel.app
taro-hooks-innocces.vercel.app
32708ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
taro-hooks-h5 – ./
taro-hooks-h5-green.vercel.app
taro-hooks-h5-git-main-innocces.vercel.app
taro-hooks-h5-innocces.vercel.app