libreact
libreact copied to clipboard
Timers
Create <Timer> or <Reminder> components
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);
}
}