@hemyn/utils-web
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

hm-utils

跨项目通用工具整理。

package

yarn build

useCountDown

hooks中使用setInterval自动计时,由于在setInterval中无法顺利的读取useState的值,因此在setInterval中直接调用setState是无效的。解决办法是使用useRef,直接更新target.current,但是这样又会出现一个问题是:虽然target.current的值是实时更新的,但是却无法出发组件的re-render的操作。因此有了这种解决办法,在setIntervalsetState的值依赖的不再是state,改为了target.current。具体细节看代码,示例如下:

import { useCountDown } from '@hemyn/utils'

const SomeComponent = () => {
  const {
    current: countDownCurrent,  // 实时读取最新的值
    start: countDownStart,  // 开始即时,同一时刻只能有一个计时器
    stop: countDownStop     // 手动停止计时
  } = useCountDown();

  ...
      countDownStart(10, 1000, afterCountToZeroRun)
  ...
      countDownStop()

  return (...<div>{countDownCurrent}</div>...)

}

Readme

Keywords

none

Package Sidebar

Install

npm i @hemyn/utils-web

Weekly Downloads

0

Version

0.4.0

License

MIT

Unpacked Size

24.4 kB

Total Files

21

Last publish

Collaborators

  • shihao