import React from 'react' ; const DEFAULT_INTERVAL = 3000 ; export default class AjaxPoller extends React . Component { _timerId = null ; _request = null ; static defaultProps = { immediately : true , } ; constructor ( props ) { super ( props ) ; this . _intervalTime = props . time || DEFAULT_INTERVAL ; } fetch ( ) { if ( this . _request ) { return ; } this . _request = this . props . onFetch ( ) . finally ( ( ) => { this . _request = null ; } ) ; } componentDidMount ( ) { this . props . immediately && this . fetch ( ) ; this . _timerId = setInterval ( this . fetch . bind ( this ) , this . _intervalTime ) ; } componentWillUnmount ( ) { clearInterval ( this . _timerId ) ; if ( this . _request && this . _request . abort ) { this . _request . abort ( ) ; } } render ( ) { return null ; } }