React clock component I'm new to React.js and this code came from 30 Days of React - Day 7. However the given code example wasn't working so I rewrote it myself. My biggest issue here is how to make the component work without function getTime() using both setState and return. Any other suggestions are appreciated.



class Clock extends React.Component {
    constructor(props) {
        super(props);
        this.state = {currentTime: this.getTime()};
    }

    getTime() {
        const currentTime = new Date(),
        hours = currentTime.getHours(),
        minutes = currentTime.getMinutes(),
        seconds = currentTime.getSeconds(),
        ampm = hours &gt;= 12 ? 'pm' : 'am';
        this.setState({currentTime: {hours, minutes, seconds, ampm}});
        return {hours, minutes, seconds, ampm};
    }

    componentDidMount() {
        const intervalId = setInterval(this.getTime.bind(this), 1000);
        this.setState({intervalId: intervalId}); 
    }

    componentWillDisMount() {
        clearInterval(this.state.intervalId);
    }

    render() {
        const {hours, minutes, seconds, ampm} = this.state.currentTime;
        return (
            &lt;div className="clock"&gt;
                {hours == 0 ? 12 : (hours &gt; 12) ? hours - 12 : hours}:
                {minutes &gt; 9 ? minutes : `0${minutes}`}:
                {seconds &gt; 9 ? seconds : `0${seconds}`} {ampm}
            &lt;/div&gt;
        )   
    }
}

ReactDOM.render(&lt;Clock /&gt;, document.querySelector("#app"));
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"&gt;&lt;/script&gt;
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"&gt;&lt;/script&gt;

&lt;body&gt;
  &lt;div id="app"&gt;&lt;/div&gt;
&lt;/body&gt;
In think you can improve the render by following the React philosofy of writing reusable components.

If you look closer at your code:

render() {
    const {hours, minutes, seconds, ampm} = this.state.currentTime;
    return (
        &lt;div className="clock"&gt;
            {hours == 0 ? 12 : (hours &gt; 12) ? hours - 12 : hours}:
            {minutes &gt; 9 ? minutes : `0${minutes}`}:
            {seconds &gt; 9 ? seconds : `0${seconds}`} {ampm}
        &lt;/div&gt;
    )   
}


Here you have duplication in printing a digit. You have it with minutes and seconds, but I think hours should be printed the same way too.

You can introduce a display component as a plain function:

const PrintDigit = ({ digit }) =&gt;
    (&lt;React.Fragment&gt;{digit &lt; 9 &amp;&amp; '0'}{digit}&lt;/React.Fragment&gt;)


The function have props as an input parameter, that here I spread with a variable digit to document the only props expected from the component.

So your render() method could be rewritten:

render() {
    const {hours, minutes, seconds, ampm} = this.state.currentTime;
    return (
        &lt;div className="clock"&gt;
            &lt;PrintDigit digit={hours === 0 ? 12 : hours % 12} /&gt;:
            &lt;PrintDigit digit={minutes} /&gt;:
            &lt;PrintDigit digit={seconds} /&gt; {ampm}
        &lt;/div&gt;
    )   
}


Is not that big improvement, but as your code is an exercise to learn React, I think is good to start thinking in that way.

The functional component could stay just in the same file of your main component or you can put it in a different file and then import where you need it.