I tried adding a search field to search for planets. Can someone help me to fix this please?
The error I get is this: TypeError: Cannot read property 'state' of undefined handleChange C:/Users/charl/Desktop/IRONHACK/Paperbox/paperbox/src/pages/Home.js:29 26 | } 27 | 28 | handleChange(e){ // eslint-disable-next-line
29 | const planetssearchlist = this.state.planets.filter(planet => { | ^ 30 | if(planet.name){ 31 | if(planet.name.toLowerCase().includes(e.target.value.toLowerCase())){ 32 | return true
import React, { PureComponent } from 'react'
import axios from "axios";
class Home extends PureComponent {
constructor(props) {
super(props)
this.state = {
planets: []
}
}
componentDidMount(){
axios({
method: "GET",
url: "https://swapi.dev/api/planets/"
})
.then(response => {
console.log(response)
let planetslist = response.data.results;
this.setState({planets: planetslist})
})
.catch(error => {
console.log("You've made an error with the planets load charles: ",error)
})
}
handleChange(e){ // eslint-disable-next-line
const planetssearchlist = this.state.planets.filter(planet => {
if(planet.name){
if(planet.name.toLowerCase().includes(e.target.value.toLowerCase())){
return true
}
}
this.setState({
planets:planetssearchlist
})
})
}
render() {
return (
<div>
<h1>Star Wars Planets</h1>
<form>
<input placeholder="searchbar" type="text" onChange={this.handleChange}></input>
</form>
{
this.state.planets.map((planet,i) => (
<p key={i}>{planet.name}</p>
))
}
</div>
)
}
}
export default Home