Skip to content

Instantly share code, notes, and snippets.

@myogeshchavan97
Last active November 16, 2020 02:54
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 myogeshchavan97/0b409f0e8678dcb935da4e4d55f09b1b to your computer and use it in GitHub Desktop.
Save myogeshchavan97/0b409f0e8678dcb935da4e4d55f09b1b to your computer and use it in GitHub Desktop.
Counter Example Without Class Properties Syntax
import React from "react";
import ReactDOM from "react-dom";
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: 0
};
this.handleAddOne = this.handleAddOne.bind(this);
}
handleAddOne() {
this.setState(prevState => {
return {
counter: prevState.counter + 1
};
});
}
render() {
return (
<div>
<div>Counter: {this.state.counter}</div>
<button onClick={this.handleAddOne}>Add One</button>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<Counter />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment