import React from 'react'; import { Link } from 'react-router-dom' export default class CurrentVisitors extends React.Component { constructor(props) { super(props) this.state = {currentVisitors: null} } componentDidMount() { this.updateCount() this.props.timer.onTick(this.updateCount.bind(this)) } updateCount() { return fetch(`/api/stats/${encodeURIComponent(this.props.site.domain)}/current-visitors`) .then( response => { if (!response.ok) { throw response } return response.json() }) .then((res) => this.setState({currentVisitors: res})) } render() { if (this.state.currentVisitors !== null) { return ( {this.state.currentVisitors} current visitors ) } else { return null } } }