Code Monkey home page Code Monkey logo

Comments (9)

liuyib avatar liuyib commented on June 15, 2024 3

目前useTimeout(fn, delay); 返回的仅仅是一个 clear 函数,如果要为以后迭代更丰富的功能,确实应该返回成对象,方便扩展。
是不是应该放到4.x去做这件事@liuyib

是的,破坏性更改。需要 v4 做

from hooks.

liuyib avatar liuyib commented on June 15, 2024 1

其实目前已经满足你这个使用场景了:

文档里有提到:useTimeout 的时间设置为 undefined 时会停止定时器

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>
  );
};

不过比较麻烦一些。可以考虑升级一下

from hooks.

raotaohub avatar raotaohub commented on June 15, 2024 1

目前useTimeout(fn, delay); 返回的仅仅是一个 clear 函数,如果要为以后迭代更丰富的功能,确实应该返回成对象,方便扩展。
是不是应该放到4.x去做这件事@liuyib

from hooks.

liuyib avatar liuyib commented on June 15, 2024

旧 API:

const clear = useTimeout(fn, delay);

新 API:

const {
  start,
  pause,
  clear,
  isActive,
} = useTimeout(fn, delay, {
  defaultActive: false
})
  • start: (delay?: number) => void
  • pause: () => void
  • clear: () => void
  • isActive: boolean

会引入 breaking change。看看大家的意见~

另外,@npmstudy 要不分享下你的使用场景?

from hooks.

npmstudy avatar npmstudy commented on June 15, 2024

好的,这周搞定

from hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.