I'm a bit confused on where I should call an API while using React, I've been putting the call on componentDidMount and doing setState there, like so:
constructor(props) {
super(props);
this.state = {info: []};
}
componentDidMount() {
this.ObtenerVariables(this.props.enlace1, this.props.enlace2);
}
ObtenerVariables(consulta1, consulta2){
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', consulta1,false);
httpRequest.send();
var cons1 =JSON.parse(httpRequest.response);
var cantArticulos = cons1.data[0].cantidad_articulos;
httpRequest.open('GET', consulta2,false);
httpRequest.send();
var cons2 =JSON.parse(httpRequest.response);
var cantAutores = cons2.data[0].cant_autores;
this.setState({ info: [cantArticulos, cantAutores] })
}
and then just access the information like so
this.state.info[0]
however I've seen some people online say that this is not good because it will give you performance issues, which is exactly the opposite of what I want, I need the website to load faster.
Is this a bad practice and is it affecting performance of the website? What would be a better way to do this? Take into account that this website needs to make around 16 API requests.