Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save umair-syed-webjet/d476f7209cf978b3603d4689a4e905c5 to your computer and use it in GitHub Desktop.
Save umair-syed-webjet/d476f7209cf978b3603d4689a4e905c5 to your computer and use it in GitHub Desktop.
basic react component
class Button extends React.Component {
state = { counter: 0};
handleClick = () => {
this.setState({
counter:this.state.counter + 1
})
};
render() {
return (
<button onClick={this.handleClick}>
{this.state.counter}
</button>
);
}
}
ReactDOM.render(<Button />, mountNode);
@umair-syed-webjet
Copy link
Author

//setState is async so this is better
handleClick = () => {
this.setState((prevState) => {
return {counter: prevState.counter + 1 };
})
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment