JSFiddle - React, Tailwind, and code Playground

by Alexey Ukolov

JavaScript

class Preloader {
  constructor() {
    window.loadingStarted = this.loadingStarted.bind(this);
    window.loadingEnded = this.loadingEnded.bind(this);
    
    this.state = {ids: new Set()};
  }
  
  loadingStarted(id) {
    this.setState(({counter}) => ({counter: (counter || 0) + 1}))
  }
  
  loadingEnded() {
    this.setState(({counter}) => ({counter: (counter || 0) - 1}))
  }
  
  render () {
    if (this.state.counter !== null && this.state.counter <= 0) {
      return null;    
    }
    
    return '<h1/>';
  }
}