how to write state in class componetes in reat code example
Example 1: how to write statefull class in react
import React, {Component} from 'react'
class Pigeons extends Component {
constructor() {
super()
this.state = {
pigeons: []
}
}
render() {
return (
<div>
<p>Look at all the pigeons spotted today!</p>
<ul>
{this.state.pigeons.map(pigeonURL => {
return <li><img src={pigeonURL} /></li>
})}
</ul>
</div>
)
}
}
Example 2: State on React class Component
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}