Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

FetchInterval #3

Closed
danilowoz opened this issue Oct 15, 2018 · 0 comments
Closed

FetchInterval #3

danilowoz opened this issue Oct 15, 2018 · 0 comments
Assignees

Comments

@danilowoz
Copy link
Contributor

import { Component } from "react"
import PropTypes from "prop-types"

class FetchInterval extends Component {
  state = {
    intervalId: null
  }

  componentDidMount() {
    const { fetch, options } = this.props

    fetch(options)
    this.setTimer()
  }

  componentWillUnmount() {
    clearInterval(this.state.intervalId)
  }

  setTimer = () => {
    const intervalId = setInterval(this.timer, this.props.interval)
    this.setState({ intervalId })
  }

  timer = () => {
    const { fetch, options } = this.props

    fetch(options)
  }

  render() {
    return this.props.children
  }
}

FetchInterval.propTypes = {
  options: PropTypes.shape({}),
  children: PropTypes.node.isRequired,
  fetch: PropTypes.func.isRequired,
  interval: PropTypes.number
}

FetchInterval.defaultProps = {
  options: {},
  interval: 10000
}

export default FetchInterval
@danilowoz danilowoz self-assigned this Oct 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant