JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1326
  • Score
    100M100P100Q101115F
  • License MIT

A React timer higher-order component

Package Exports

  • react-timer-hoc

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-timer-hoc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version Build Status

React timer component (higher-order)

A React higher-order timer component

Keep your components simple, testable and composable by using higher-order components. This higher-order timer component will re-render your component at the desire rate (in milliseconds).

This higher-order component takes care of when to call render on your component, so your component has only to care about the rendering logic.

A higher-order component is just a function that takes an existing component and returns another component that wraps it.

Read about higher-order components here (applies to deku as well): Mixins Are Dead. Long Live Composition.

Demo: http://jsbin.com/nalixa/edit?html,js,output

Applications

  • Countdowns (time remaining)
  • Timers (time elapsed)
  • Forcing updates / refresh of time-based contents

Features

  • Stop and resume a timer
  • Change its delay on the fly
  • Synchronize with a timestamp

Installation

npm install --save react-timer-hoc

Usage

Create a new component by wrapping your component with timer HOC. Alongside the properties you specify, the created component will receive a timer property containing:

  • A tick value (incremented)
  • The specified delay value
  • stop, resume and setDelay functions

Important notice with ES5

babel 6 changed the way transpiled default exports work. See Babel 6 changes how it exports default on stack overflow.

// ES5
var timer = require('react-timer-hoc').default;
import React from 'react';
import ReactDOM from 'react-dom';

function myComponent({ timer }) {
    return <div>Started { timer.tick * timer.delay }ms ago.</div>
}

const Timer1 = timer(1000)(myComponent);
const Timer2 = timer(2000)(myComponent);

ReactDOM.render(
    <div>
        <Timer1 />
        <Timer2 />
    </div>,
    document.getElementById('app')
);