-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
能不能把useTimeout升级一下? #2537
Comments
其实目前已经满足你这个使用场景了:
import React, { useState } from 'react';
import { useInterval } from 'ahooks';
export default () => {
const [count, setCount] = useState(0);
const [delay, setDelay] = useState<number | undefined>(1000);
const isActive = delay !== undefined;
const start = () => setDelay(1000);
const pause = () => setDelay(undefined);
const clear = useInterval(() => {
setCount(count + 1);
}, delay);
return (
<div>
<button onClick={start}>start</button>
<button onClick={pause}>pause</button>
<button onClick={clear}>clear</button>
<div>isActive: {`${isActive}`}</div>
<div>count: {count}</div>
</div>
);
}; 不过比较麻烦一些。可以考虑升级一下 |
旧 API: const clear = useTimeout(fn, delay); 新 API: const {
start,
pause,
clear,
isActive,
} = useTimeout(fn, delay, {
defaultActive: false
})
会引入 breaking change。看看大家的意见~ 另外,@npmstudy 要不分享下你的使用场景? |
目前useTimeout(fn, delay); 返回的仅仅是一个 clear 函数,如果要为以后迭代更丰富的功能,确实应该返回成对象,方便扩展。 |
是的,破坏性更改。需要 v4 做 |
好的,这周搞定 |
还有一些新增修改,比如start方法,需要把参数透传给具体fn。 |
补充写法
|
完全没有必要做 |
会让useTimeout更难理解 |
希望能把 |
用法示意
如果需要,我把pr和测试提上来。
The text was updated successfully, but these errors were encountered: