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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-heartbeat

v1.0.0

Published

react-heartbeat React component

Downloads

1,067

Readme

react-heartbeat

Travis npm package Coveralls

React Heartbeat is a simple React Component that will call a function that you provide at a regular interval. It should stay accurate within a few milliseconds as long as you run it. React Heartbeat will continue to call the function you pass to it as long as it is rendered, to stop it, just stop rendering React Heartbeat.

Usage

To use React Heartbeat with your component, first import it:

import Heartbeat from 'react-heartbeat';

Then render it in your component’s render function:

render() {
    return (
        <Heartbeat heartbeatFunction={this.count} heartbeatInterval={1000} />
    );

React Heartbeat accepts 2 props:

  1. heartbeatFunction: this is the function you want to call after each heartbeatInterval.
  2. heartbeatInterval: this is the time interval in milliseconds you want to be between each call to heartbeatFunction

heartbeatInterval is optional. If you do not provide this prop the default is 1000 milliseconds (1 second).

Starting/Stopping React Heartbeat

There are several ways you can manage starting and stopping React Heartbeat from calling the function you pass to it. Here is one way:

In the state of your component add a 'paused' property as a boolean.

this.state = {
  paused: true,
  timer: 0
};

Then in the render function set a variable using the ternary operator to 'null' if paused is true, and set it to the React Heartbeat component if paused is false. Then render this variable in your component like this:

render() {
    const heartbeat =
        this.state.paused === true ? null : (
        <Heartbeat heartbeatFunction={this.count} heartbeatInterval={50} />
        );
    return (
        <Fragment>
        {heartbeat}
        </Fragment>
    );
}

For an example of React Heartbeat used in a web app see my Pomodoro Timer. The source code for this project is here. Note: this project uses React Heartbeat as a component, not as a npm module. I created React Heartbeat while developing this project, then decided to make it an npm module.

How it works.

React Heartbeat records the system time when you first call it in state, then it schedules a call to the function you provide as a prop using setTimeout at the interval you specify as a prop. When React Heartbeat calls your function, it compares the system time when it called your function to the time it should have called your function, then adjusts the next call to your function so that it will be called at twice the interval you provided from the time you first rendered React Heartbeat in your component. It continues comparing the time your function was called to the time it should have been called and making adjustments as long as you continue to render React Heartbeat in your component.

The source code for React Heartbeat is here.