title: React. getTime() function does not update the state
body: <p>I' trying to update the state of the component <code>Demoss</code> by external function <code>getTime()</code>. I want to start update the time in the <code>state time</code> on the page load. And to make it real, I have invoke it in the <code>componentDidMount</code>. But for some reasons it does not happen. </p>
  
  <pre><code>import React from 'react';
  import ReactDOM from 'react-dom';
  import PropTypes from 'prop-types';
  import './index.css';
  
  function getTime() {
    let a = setInterval(() =&gt;
      {new Date()}, 1000 
    );
    return a;
  }
  
  class Demoss extends React.Component {
    constructor(props) {
      super(props);
  
      this.state = {
        time: '',
        timer: false
      };
  
      this.handleClick = this.handleClick.bind(this);
    }
  
    componentDidMount() {
      this.setState({
        time: getTime() // invoke setInterval state update, but it does not work
      });
    }
  
    componentWillUnmount() {
      clearInterval(getTime());
    }
  
    handleClick() {
      this.setState(prevState =&gt; ({
          timer: !prevState.timer
      }));
  
      if (this.state.timer === false) {
        clearInterval(this.timerId);
      } else if (this.state.timer === true) {
      }
    }
  
    render() {
      return (
        &lt;div&gt;
          &lt;p&gt;{this.state.time.toString()}&lt;/p&gt;
          &lt;button onClick={this.handleClick}&gt;&lt;Dimes timer={this.state.timer}/&gt;&lt;/button&gt;
        &lt;/div&gt;
      );
    }
  }
  
  class Dimes extends React.Component {
    render() {
      if (this.props.timer === true) {
        return (
          &lt;React.Fragment&gt;
            Start timer again!
          &lt;/React.Fragment&gt;
        );
      } else {
        return (
          &lt;React.Fragment&gt;
            Start timer!
          &lt;/React.Fragment&gt;
        );      
      }
  
    }
  }
  
  ReactDOM.render(
  &lt;Demoss /&gt;,
  document.getElementById('root'));
  </code></pre>