npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-time-hoc

v0.1.13

Published

暴露一个创建高阶组件的函数,该函数返回的组件会添加上 timer 属性。 最典型的的应用场景就是手机验证码。

Readme

react-time-hoc

暴露一个创建高阶组件的函数,该函数返回的组件会添加上 timer 属性。
最典型的的应用场景就是手机验证码。

import withTimer from "react-time-hoc";
const LoginTimerComponent = withTimer(LoginComponent, options);

options 有如下属性:

  • interval
    计时间隔,默认 1000ms
  • start
    初始值,默认值 60
  • step
    步距, 默认值 -1
  • end
    目标值,默认值 0

timer 属性有如下属性:

  • isTiming: boolean
    是否在计时
  • value: number
    当前的值,数组
  • start(option)
    开始计时
  • cancel()
    暂停/取消计时
  • continue()
    继续计时
  • reset(autoStart?:boolean)
    重置

安装

npm install react-time-hoc

使用

import withTimer from "react-time-hoc";
import LoginComponent from "./LoginComponent";

const LoginTimerComponent = withTimer(LoginComponent, options);

示例

App.js

import React, { Component } from "react";
import "./App.css";
import TimingComponent from "./TimingComponent";

import withTimer from "react-time-hoc";

const TimingCom = withTimer(TimingComponent, {
  interval: 100,
  start: 30,
  end: 10
});

class App extends Component {
  render() {
    return (
      <div
        className="App"
        style={{
          marginTop: 120
        }}
      >
        <TimingCom />
      </div>
    );
  }
}

export default App;

TimingComponent.js

import React from "react";

export default class TimingComponent extends React.Component {
    state = {
        isGetting: false
    };

    componentDidMount() {
        const { timer } = this.props;
        if (timer) {
            // timer.start();
        }
    }
    onGetCode = () => {
        this.setState({ isGetting: true });
        setTimeout(() => {
            this.props.timer.start();
            this.setState({
                isGetting: false
            });
        }, 1500);
    };

    render() {
        const { value, isTiming } = this.props.timer;
        const { isGetting } = this.state;

        const valueText = isGetting ? "获取中" : isTiming ? "剩余时间" + value + "s" : "获取验证码";

        return (
            <div>
                <p>计时:{value}</p>
                <p>isTiming: {isTiming + ""}</p>

                <input disabled={isTiming || isGetting ? true : false} type="button" value={valueText} onClick={this.onGetCode} />
                <br />
                <input type="button" value="开始" onClick={() => this.props.timer.start()} />
                <input type="button" value="停止" onClick={() => this.props.timer.cancel()} />
                <input type="button" value="继续" onClick={() => this.props.timer.continue()} />
                <input type="button" value="重置" onClick={() => this.props.timer.reset()} />
                <input type="button" value="重置并继续" onClick={() => this.props.timer.reset(true)} />
            </div>
        );
    }
}

示例图