I've got a component who is child from another that when is mounted called a function called getData(). This function after make some queries on the databse call to the method setState but this call doesn't work because doesn't change the value of loaded and is no exectuted the callback.
The code of the component is :
import React, {Component} from "react";
import {Determinator, MultiLang} from "react-multi-language";
import { getDataFromServer } from "./Functions";
import {GAME_STATS_BY_TEAM, TEAM_COMPETITION_STATS} from "../FEBCOM/Querys";
class TeamTableStandardStats extends Component {
constructor(props){
super();
this.props = props;
this.state = {
loaded: false,
lang: this.props.isFEB ? "es" : "en"
};
}
componentDidMount(){
this.getData();
}
async getData(){
let params = [
{"###id_team_club###": this.props.id_team},
{"###id_competition###": this.props.id_competition}
];
let team_games_stats = {};
let team_comp_stats = {};
if (this.props.isFEB){
team_games_stats = await getDataFromServer(process.env.URL_FEB_API, GAME_STATS_BY_TEAM, params);
team_comp_stats = await getDataFromServer(process.env.URL_FEB_API, TEAM_COMPETITION_STATS, params);
}else{
team_games_stats = await getDataFromServer(process.env.URL_FIBA_API, GAME_STATS_BY_TEAM, params);
team_comp_stats = await getDataFromServer(process.env.URL_FIBA_API, TEAM_COMPETITION_STATS, params);
}
console.log("getData() => Before setState");
this.setState = ({
loaded: true,
// team_games_stats: team_games_stats.data.game_stats_by_team,
// team_comp_stats: team_comp_stats.data.team_comp_stats
}, () => {
console.log("loaded: " + this.state.loaded);
});
console.log("getData() => After setState");
}
render(){
return(
<div>
<span>loaded: {this.state.loaded ? "YES" : "NO"}</span>
{
(this.state.loaded) ?
<div>
<h1>Tabla - {this.props.isFEB ? "Es FEB" : "Es FIBA"}</h1>
<span>id_team: {this.props.id_team} - id_competition: {this.props.id_competition}</span>
<span>Resumen: {this.state.team_comp_stats.length}</span>
</div>
:
<h5>
<Determinator>
{{
es: "Cargando datos ...",
en: "Loading data ..."
}}
</Determinator>
</h5>
}
<MultiLang lang = {this.state.lang} />
</div>
)
}
}
module.exports.TeamTableStandardStats = TeamTableStandardStats;
These code when is exected writes in console.log:
How you can see the log which belongs to this instrucctions is not executed:
console.log("loaded: " + this.state.loaded);
And how loaded is false you can see this in the view:
So, I have no idea why is not executing the method setState.
What am I doing wrong?