Created
July 26, 2018 19:51
-
-
Save jonbesga/166385ef2cb2fd22111505ec6a80b624 to your computer and use it in GitHub Desktop.
React example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import './index.css'; | |
import registerServiceWorker from './registerServiceWorker'; | |
class Clock extends React.Component{ | |
constructor(props) { | |
super(props); | |
this.state = { date: new Date(), clicks: 0 }; | |
this.incrementItem = this.incrementItem.bind(this); | |
} | |
componentDidMount() { | |
this.timerID = setInterval( | |
() => this.tick(), | |
1000 | |
); | |
} | |
tick() { | |
this.setState({ date: new Date() }); | |
} | |
incrementItem(){ | |
this.setState({ clicks: this.state.clicks + 1 }); | |
} | |
render(){ | |
return( | |
<div> | |
<h1>Hello, {this.props.name}!</h1> | |
<h2>It is {this.state.date.toLocaleTimeString()}</h2> | |
<button onClick={this.incrementItem}>Click to increment by 1</button> | |
<h3>{this.state.clicks}</h3> | |
</div> | |
) | |
} | |
} | |
const element = ( | |
<div> | |
<Clock name="alex" /> | |
<Clock name="jon" /> | |
<Clock name="thomas" /> | |
</div> | |
); | |
ReactDOM.render(element, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment