title: React eventsource is not closed
body: <p>I'm quite new to web development and server-sent-events in particular so probably I'm missing something obvious. I'm trying to display a logger on a React page with server-sent-events, which works fine, but I cannot close the eventSource. The server continuously receives requests after eventSource.close() was called.</p>
  <pre><code>  const [eventSource, setEventSource] = React.useState(new EventSource(&quot;http://localhost:5001/logs&quot;))
    const [logs, setLogs] = React.useState([])
  
    React.useEffect(() =&gt; {
      eventSource.onmessage = e =&gt; updateLogs((e.data))
  
      return (() =&gt; eventSource.close() )
    }, [])
  
    const updateLogs = (entry) =&gt; {
      setLogs(logs =&gt; [...logs, entry])
      if (entry === 'finished'){
        eventSource.close()
        console.log('closed eventsource')
        setEventSource(null)
      }
    }
  </code></pre>
  <p>The console logs &quot;closed eventsource&quot; as expected and the logs hook is not updated any longer, but the requests keep getting fired from somewhere. What am I missing?</p>
  <p>The requests are handled by a flask server like this (just send some dummy logs):</p>
  <pre><code>def log_progress():
    for i in range(10):
      message = 'data: step {} \n\n'.format(i)
      yield message
      time.sleep(1)
    yield 'data: finished\n\n'
  
  @app.route(&quot;/logs&quot;)
  def stream_logs():
      return = Response(log_progress(), mimetype=&quot;text/event-stream&quot;)
  </code></pre>
  <p>The server log:</p>
  <pre><code>&lt;Response streamed [200 OK]&gt;
  127.0.0.1 - - [02/Oct/2020 11:00:22] &quot;GET /logs HTTP/1.1&quot; 200 -
  &lt;Response streamed [200 OK]&gt;
  127.0.0.1 - - [02/Oct/2020 11:00:22] &quot;GET /logs HTTP/1.1&quot; 200 -
  &lt;Response streamed [200 OK]&gt;
  127.0.0.1 - - [02/Oct/2020 11:00:22] &quot;GET /logs HTTP/1.1&quot; 200 -
  &lt;Response streamed [200 OK]&gt;
  127.0.0.1 - - [02/Oct/2020 11:00:23] &quot;GET /logs HTTP/1.1&quot; 200 -
  &lt;Response streamed [200 OK]&gt;
  127.0.0.1 - - [02/Oct/2020 11:00:23] &quot;GET /logs HTTP/1.1&quot; 200 -
  // this keeps on forever until page is closed
  </code></pre>