libreact icon indicating copy to clipboard operation
libreact copied to clipboard

Timers

Open streamich opened this issue 7 years ago • 1 comments

Create <Timer> or <Reminder> components

streamich avatar Feb 13 '18 10:02 streamich

class Timer extends Component {
  static propTypes = {
    children: Types.func.isRequired,
    ...timerProps
  };

  static defaultProps = {
    onTime: noop
  };

  state = {
    timed: false
  };

  componentDidMount () {
    this.timeout = setTimeout(() => {
      this.props.onTime();
      this.setState({timed: true});
    }, this.props.time);
  }

  componentWillUnmount () {
    clearTimeout(this.timeout);
  }

  render () {
    if (process.env.NODE_ENV !== 'production') {
      if (typeof this.props.children !== 'function') {
        throw new TypeError('Children in <Timer/> must be a function.');
      }
    }

    return this.props.children(this.state.timed);
  }
}

streamich avatar Feb 13 '18 10:02 streamich