stateful vs stateless react 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: stateful component vs stateless component
class Parent extends Component {
constructor() {
super()
this.state = {
books: [],
favoriteAuthors: []
}
}
render() {
return (
<div>
<Books books={this.state.books} />
<FavoriteAuthors favoriteAuthors={this.state.favoriteAuthors} />
</div>
)
}
}