I am working with a random object in an array, however I am not able to render it
I tried rendering using {planet.name}, and return (name is not defined):
return (
<div className="App">
<div>{planet.name}</div>
<button onClick={this.renderPlanet}>Next</button>
</div>
)
i tried redering using {planet}, and return (Objects are not valid as a React child (found: object with keys)):
return (
<div className="App">
<div>{planet}</div>
<button onClick={this.renderPlanet}>Next</button>
</div>
)
App.JS
import React, { Component } from 'react';
import './App.css';
class App extends Component {
state = {
data: [],
chosenPlanet: 0,
}
componentDidMount() {
const url = 'https://swapi.co/api/planets/'
fetch(url)
.then(response => response.json())
.then(response => {
this.setState({
data: response.results,
})
})
}
render() {
const index = Math.floor(Math.random() * this.state.data.length)
const planet = this.state.data[index]
console.log(planet)
return (
<div className="App">
<div>{this.planet.name}</div>
<button onClick={this.renderPlanet}>Next</button>
</div>
)
}
}
export default App;
i need to return:
<div className="app-container">
<div>
<h1>{planet.name}</h1>
</div>
<div className="about-container">
<span>Population: {planet.population}</span>
<span>Climate: {planet.climate}</span>
<span>Terrain: {planet.terrain}</span>
<br />
<span>Quantidade de Filmes {planet.films.length}</span>
</div>
</div>
console.log(planet)before render and tell what it looks like?planetlooks like ? can you post your console in question ?