react-livestamp

Auto updating timeago text to your timestamped for React

Usage no npm install needed!

<script type="module">
  import reactLivestamp from 'https://cdn.skypack.dev/react-livestamp';
</script>

README

React Livestamp

Auto updating timeago text to your timestamped for React

Installation

npm install react-livestamp --save

Basic Usage

import React from 'react';
import Livestamp from 'react-livestamp';

// Dummy date
const end_date = new Date();

// add 5 hours.
end_date.setHours(end_date.getHours()+5);

class App extends React.Component {
  render() {
    return (
      <Livestamp end={end_date} />
    )
  }
}

Props

end (required)

Accepts a Date object, a String format, or a Number timestamp.


const end_time = new Date(); // Object instanceof date
const str_end = end_date.toString() // Tue Jun 14 2016 21:25:41 GMT+0300 (EEST)
const timestamp_end = end_date.getTime() // 1465928741178

<Livestamp end={end_time} />
<Livestamp end={str_end} />
<Livestamp end={timestamp_end} />

interval (optional)

The second parameter for setInterval is optional with default value of 1000.

renderStamp (optional)
class App extends React.Component {
  renderStamp({ days, hours, minutes, seconds }) {
    return (
      <div className="react-livestamp">
        <b>{days} g {hours} s { minutes } dk {seconds} sn</b>
      </div>
    )
  }
  
  render() {
  
    return (
    
      // Default renderStamp template
      <Livestamp end={end_time} renderStamp={({ days, hours, minutes, seconds }) => (
        <div className="react-livestamp">
          <b>{days} g {hours} s { minutes } dk {seconds} sn</b>
        </div>
      )}/>

      // or may be in this way:
      <Livestamp renderStamp={this.renderStamp}>
    )
  }
}

renderExpired (optional)

If the livestamp ends it run this.


class App extends React.Component {
  renderExpired({ days, hours, minutes, seconds }) {
    return (
      <div className="react-livestamp">
        Date Expired
      </div>
    )
  }
  
  render() {
  
    return (
    
      // Default renderStamp template
      <Livestamp end={end_time} renderExpired={() => (
        <div className="react-livestamp">
          Date Expired
        </div>
      )}/>

      // or may be in this way:
      <Livestamp renderExpired={this.renderExpired}>
    )
  }
}

Development

npm install
npm start # watch and build.