Understanding the Basics of State in React

I know, I know…but you have to learn it!

What is state?

from Reactjs.org
class Jar extends React.Component {
constructor() {
super()
this.state = {
count: 0
}
}
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button>Add $$ to Jar</button>
</div>
)
}
}
handleClick() {
this.setState(prevState => {
return {
count: prevState.count + 1
}
})
}
class Jar extends React.Component {
constructor() {
super()
this.state = {
count: 0
}
}
handleClick() {
this.setState(prevState => {
return {
count: prevState.count + 1
}
})
}
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={this.handleClick}>
Add $$ to Jar</button>
</div>
)
}
}

Software Engineer in the making.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store