0
talk boys. I’m having a beginner problem... I’m new to reactjs and wanted to know how to do it: as soon as I click the button it returns me the data of the api inside a DIV
ex: (article, Section and etc...)
import React, { Component } from 'react';
import Api from '../API/index';
import './main.css'
class Main extends Component {
constructor(props){
super(props);
this.state = {
planets : [],
}
}
componentDidMount(){
this.loadItens();
}
loadItens = async () => {
const response = await Api.get('/planets');
this.setState({ planets: response.data.results });
}
render() {
return(
<div className="product-list mx-auto d-block text-center mt-5">
{this.state.planets.map(planetz => (
<article key={planetz._id} className="product-list mx-auto d-block text-center mt-5">
<p> Nome: {planetz.name} </p>
<p> Clima: {planetz.climate} </p>
<p> Terreno: {planetz.terrain} </p>
<p> População: {planetz.population} </p>
<p> Participação em filmes: {planetz.films.length} </p>
<button type="submit" onClick={this.loadItens}> Procurar Planetas </button>
</article>
))}
</div>
);
};
}
export default Main;
John, could edit your question and clarify your problem / difficulty ?
– NoobSaibot
yes, of course! has already been edited... thank you.
– Joao Paulo Gardiano
But aren’t you already doing it? In your
onClick
you invoke the functionloadItens
.loadItens
search for data on your server, and then invoke thesetState
to change thestate
and re-render the component. It’s all right. The only problem is that you put yourbutton
inside the loop that mounts the HTML of the planets, so if you don’t have any planets on yourstate
, you will not enter the loop and no button will be rendered.– Andre
@user140628 No. I called the map() parameter inside the article that returned me the api items. I wanted to know how to make sure that as soon as I clicked on this button he returned these parameters to me without having to call them in html. vlw!
– Joao Paulo Gardiano
Then I still don’t understand exactly what you’re trying to do. I understand that you are a beginner, but the terms you are using to describe this problem make no sense and this is making communication difficult.
parâmetro
is a value passed to a function "calling the map parameter" is a sentence that makes no sense. Another thing is that I’m not understanding is what you mean by "return these parameters without needing to call them"– Andre
ex: I returned inside the article <p> Name: {planetz.name} </p> I wanted as soon as I clicked on the button to return it to me without having to use that <p> Name: {planetz.name} </p> manually.
– Joao Paulo Gardiano
What do you mean by "use <p> Name: {planetz.name} </p> manually"? Sorry, but we seem to be speaking different languages.
– Andre
You need to apologize no.. All right! "manually" don’t need to call you in article.
– Joao Paulo Gardiano
when clicking on the button wanted me to return this map() with the items contained in the api that in this case would be, planet name, climate, terrain, planet population. all this just by clicking the button.
– Joao Paulo Gardiano