0

OS: Ubuntu 18.10

Node: v12.13.1

Browser: 79.0.3945.88 (Official Build) (64-bit)

I am new to react but experienced with Javascript. I am trying to create a simple "clicker" game as my first react app. I have set a 100ms timer and I believe I'm updating the state correctly. Every 100ms I update and display a counter to the DOM, that's it.

The problem I'm having is that my app is freezing chrome randomly after ~20 seconds. When the browser freezes the only way to get it back is to click another tab. When I look at chrome performance tab it seems like it may be garbage collection related.

I've tried to boil this down to the bare minimum however the problem gets worse if the render() has a few more elements (like 10).

I made a default app with:

npx create-react-app my-app

And these are my only changes:

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      count: 0
    };

    this.handleStop = this.handleStop.bind(this);
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.tick(),
      100
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  handleStop(e) {
    clearInterval(this.timerID);
  }

  tick() {
    const s = this.state;
    this.setState({
      count: s.count+1
    });
  }

  render() {
    let s = this.state;
    return (
      <div>
        <h1>{s.count}</h1>
        <button onClick={this.handleStop}>Stop Counter</button><br/>
      </div>
    );
  }
}

Full code: https://github.com/esromneb/timer-fail

Normally we would expect 100ms frames however when it hangs I get a 14 second frame. I have to click another tab to get it back.

Hung app in Chrome

What am I doing wrong? Is react not meant for 100ms updates forever? Thanks.

benathon
  • 7,455
  • 2
  • 41
  • 70
  • I just tried your exact code in my personal machine and everything is fine after 2 minutes. what ever problem your having is not related to your code. – punjira Jan 01 '20 at 11:00
  • Do you see "Listeners" constantly climbing in the performance tab? – benathon Jan 01 '20 at 11:12
  • 1
    I didn't see such thing but someone else did! take a look at [this](https://stackoverflow.com/questions/48201104/listeners-in-chrome-dev-tools-performance-profiling-results) – punjira Jan 01 '20 at 11:57
  • 1
    seems like is a create-react-app bug – punjira Jan 01 '20 at 11:57

0 Answers0