Skip to content

Instantly share code, notes, and snippets.

@MHenderson
Last active July 2, 2019 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MHenderson/aeff7e1c25ea6664dbce7154e4fc1bc8 to your computer and use it in GitHub Desktop.
Save MHenderson/aeff7e1c25ea6664dbce7154e4fc1bc8 to your computer and use it in GitHub Desktop.
react-hello-world
<div id="timer-example"></div>
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
tick() {
this.setState(state => ({
seconds: state.seconds + 1
}));
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
<div>
Seconds: {this.state.seconds}
</div>
);
}
}
ReactDOM.render(
<Timer />,
document.getElementById('timer-example')
);
<script src="//unpkg.com/react/umd/react.development.js"></script>
<script src="//unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment