Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-interval

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-interval

Safe React wrapper for setInterval

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1K
decreased by-24.2%
Maintainers
1
Weekly downloads
 
Created
Source

react-interval

Circle CI Coverage Status Dependency Status devDependency Status

Safe React wrapper for setInterval

Installation

npm install --save react-interval

Usage

Quicksart

Start counting on render

import React from 'react';
import ReactInterval from 'react-interval';

const App = React.createClass({
  getInitialState() {
    return {count: 0};
  },

  inc() {
    this.setState({count: this.state.count + 1});
  },

  render() {
    const {count} = this.state;

    return (
      <div>
        {count}
        <ReactInterval timeout={1000} enabled={true} callback={this.inc} />
      </div>
    );
  }
});


React.render(<App />, document.body);

Full example

Chang timeout on the fly, start and stop counting

import React from 'react';
import ReactInterval from 'react-interval';

const App = React.createClass({
  getInitialState() {
    return {
      enabled: false,
      timeout: 1000,
      count: 0
    };
  },

  inc() {
    this.setState({count: this.state.count + 1});
  },

  render() {
    const {timeout, enabled, count} = this.state;

    return (
      <div>
        <ReactInterval {...{timeout, enabled}} callback={this.inc} />

        <input type="number" step="200" min="200" max="5000" value={this.state.timeout}
          onChange={({target: {value}}) => this.setState({timeout: parseInt(value, 10)})} />&nbsp;

        <button disabled={enabled} onClick={() => this.setState({enabled: true})}>
          Start</button>&nbsp;

        <button disabled={!enabled} onClick={() => this.setState({enabled: false})}>
          Stop</button>&nbsp;

        {count}
      </div>
    );
  }
});


React.render(<App />, document.body);

Options

callback: React.PropTypes.func.isRequired,
enabled: React.PropTypes.bool,
timeout: React.PropTypes.number

callback: PropTypes.func.isRequired

Function repeatedly called after timeout

enabled: PropTypes.bool (default: false)

Should start timer?

timeout: PropTypes.number (default: 1000)

Timeout before each callback call

Development and testing

npm install
npm start

Then

open http://localhost:8080

Demo

http://nkbt.github.io/react-interval/example

License

MIT

Keywords

FAQs

Package last updated on 17 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc