4.1.0-alpha4 • Published 7 years ago

react-time-sync v4.1.0-alpha4

Weekly downloads
166
License
MIT
Repository
github
Last release
7 years ago

react-time-sync

npm (scoped) CircleCI Coverage Status

A React library to synchronize timers across an application

Usage

When using any timed component or hook, a <TimeProvider> component needs to be in the top of your component hierarchy

Example:

import { TimeProvider } from 'react-time-sync';

const App = ({ content }) => {
  return (
    <div>
      <TimeProvider>
        {content}
      </TimeProvider>
    </div>
  )
};

useCountdown hook

A custom hook which returns the time left until a certain millisecond UTC timestamp is reached

Example:

import { useCountdown } from 'react-time-sync';

const MyComponent = ({ until }) => {
  const timeLeft = useCountdown({ until });
  return <div>{timeLeft > 0 ? `${timeLeft} seconds left` : 'Done!'}</div>;
}

Input

The useCountdown hook expects an object with the following properties as the single argument:

until - A UTC millisecond timestamp until when the countdown should run (default: 0) interval - one of TimeSync.SECONDS, TimeSync.MINUTES, TimeSync.HOURS, TimeSync.DAYS (default: TimeSync.SECONDS)

useTime hook

A custom hook which returns the current time rounded to the specified interval

Example:

import { useTime } from 'react-time-sync';

const MyComponent = () => {
  const currentTime = useTime();
  return <div>{`The current time is: ${currentTime}`}</div>;
}

Input

The useTime hook expects an object with the following properties as the single argument:

unit - The number of units of interval (default: 1) interval - one of TimeSync.SECONDS, TimeSync.MINUTES, TimeSync.HOURS, TimeSync.DAYS (default: TimeSync.SECONDS)

A component that accepts render props to periodically re-render its children with the time left until a certain millisecond UTC timestamp

Example:

import { Countdown } from 'react-time-sync';

const MyComponent = ({ until }) => {
  return (
    <Countdown until={until}>
    {({ timeLeft }) => (
      <div>{timeLeft > 0 ? `${timeLeft} seconds left` : 'Done!'}</div>
    )}
    </Countdown>
  )
}

const until = Date.now() + 5000;

ReactDOM.render(<MyComponent until={until} />, ...);

Props

until - A UTC millisecond timestamp until when the countdown should run (required) interval - one of TimeSync.SECONDS, TimeSync.MINUTES, TimeSync.HOURS, TimeSync.DAYS (default: TimeSync.SECONDS)

A component that accepts render props to periodically re-render its children with the current time rounded to the specified interval

Example:

import { Timed } from 'react-time-sync';

const MyComponent = () => {
  return (
    <Timed>
    {({ currentTime }) => (
      <div>{`The current time is: ${currentTime}`}</div>
    )}
    </Timed>
  )
}

Props

unit - The number of units of interval (default: 1) interval - one of TimeSync.SECONDS, TimeSync.MINUTES, TimeSync.HOURS, TimeSync.DAYS (default: TimeSync.SECONDS)

connectTime()()

A higher order component meant to be used in combination with redux.

Example:

import { connectTime, SECONDS } from 'react-time-sync';

const timeSlotsSelector = createSelector(
  currentTime => currentTime,
  currentTime => [currentTime - 1, currentTime + 1]
)

function mapStateToProps() {
  const timeSlots = timeSlotSelectors(currentTime)
  return {
    timeSlots
  };
}

const timerConfig = {
  unit: 1,
  interval: SECONDS
}

export default connectTime(timerConfig)(connect(mapStateToProps)(MyComponent));

timerConfig properties

unit - The number of units of interval (default: 1) interval - one of TimeSync.SECONDS, TimeSync.MINUTES, TimeSync.HOURS, TimeSync.DAYS (default: TimeSync.SECONDS)

5.2.1

4 years ago

5.2.0

4 years ago

5.1.1

4 years ago

5.1.0

5 years ago

5.0.11

6 years ago

5.0.10

6 years ago

5.0.9

6 years ago

5.0.8

6 years ago

5.0.7

6 years ago

5.0.6

6 years ago

5.0.5

7 years ago

5.0.4

7 years ago

5.0.3

7 years ago

5.0.2

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.4.0

7 years ago

4.3.2

7 years ago

4.3.1

7 years ago

4.3.0

7 years ago

4.2.0

7 years ago

4.2.0-alpha3

7 years ago

4.2.0-alpha2

7 years ago

4.1.1

7 years ago

4.2.0-alpha1

7 years ago

4.1.0

7 years ago

4.1.0-alpha4

7 years ago

4.0.1

7 years ago

4.1.0-alpha3

7 years ago

4.1.0-alpha2

7 years ago

4.1.0-alpha1

7 years ago

4.0.0

7 years ago

3.1.1-alpha1

7 years ago

3.0.2

7 years ago

3.1.0-alpha2

7 years ago

3.1.0-alpha

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.0

7 years ago

2.0.0-alpha.3

7 years ago

2.0.0-alpha.2

8 years ago

2.0.0-alpha.1

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.1

8 years ago