I'm consuming the pokeapi for a personal project and wanted to learn React while doing it. Once managed to retrieve the desire information from the api, like this:
handleSubmit(event) {
axios.get('https://pokeapi.co/api/v2/pokemon/' + this.state.input_id).then(r => {
var data = r.data;
var types = [];
for (let i = 0; i < data.types.length; i++) types.push(data.types[i].name);
const pokemon = {
id: data.id,
name: data.name,
sprite: data.sprites.front_default,
height: data.height,
weight: data.weight,
types: types,
};
this.setState({
pokemon: pokemon
});
}).catch(e => {
console.log(e);
}); // axios
event.preventDefault();
} // handleSubmit
All i want is to render this pokemon object. This is the way i'm doing it right now:
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
Pokemon's id
<input type="text" value={this.state.input_id} onChange={this.handleChange} />
</label>
<input type="submit" value="Find!"/>
</form>
<code>{this.state.pokemon}</code>
</div>
);
} // render
Of course, this throws the next error:
Objects are not valid as a React child (found: object with keys {id, name, sprite, height, weight, types}). If you meant to render a collection of children, use an array instead.
Is there a way to render this object inside the table? I guess it would be without saving the data inside the state but i don't know how to do so.