0.4.18 • Published 9 months ago

@hemyn/utils v0.4.18

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

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>...)

}
0.4.17

9 months ago

0.4.18

9 months ago

0.4.6

12 months ago

0.4.0

3 years ago

0.1.28

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.26

4 years ago

0.1.27

4 years ago

0.1.20

4 years ago

0.1.23

4 years ago

0.1.24

4 years ago

0.1.25

4 years ago

0.1.18

4 years ago

0.1.19

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.9

4 years ago

0.1.2

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.4

4 years ago

0.1.6

4 years ago

0.1.1

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago