I have problem to call one function in another two functions. Two functions, where I will call function is same. I am using mapping. All three functions I added in constructor, but I have no idea why this happend.
Here is my code:
class medicalDataForAll extends React.Component{
constructor(props) {
super(props)
this.getLastWeekHeartRate = this.getLastWeekHeartRate.bind(this)
this.getLastWeekHeartRateX = this.getLastWeekHeartRateX.bind(this)
this.getLastWeekHeartRateY = this.getLastWeekHeartRateY.bind(this)
}
getLastWeekHeartRate() {
var daysValues = [];
this.getLastWeekHeartRateDatesXY().forEach((a) => {
if (!this[a[0].day]) {
this[a[0].day] = { day: a[0].day, value: 0};
daysValues.push(this[a[0].day]);
}
this[a[0].day].value += a[0].value;
this[a[0].day].month = a[0].month;
}, Object.create(null));
var days = this.getLastWeekHeartRateDatesXY().map((a) => a[0].day)
var daysCounts = [...new Set(days)].map(e =>({
day: e,
count: days.filter(n => n===e).length }))
var newArray = daysValues.map(function(value, index) {
return {
xAxe: "" + daysValues[index].day + '/' + daysValues[index].month + "",
yAxe: parseInt(daysValues[index].value / daysCounts[index].count)
}
});
//console.log(newArray.reverse())
return newArray.reverse()
}
getLastWeekHeartRateX() {
const value = this.getLastWeekHeartRate().map((a) => a.xAxe)
console.log(value)
return value
}
getLastWeekHeartRateY() {
const value = this.getLastWeekHeartRate().map((a) => a.xAxe)
console.log(value)
return value
}
Problem is showing getLastWeekHeartRateY(), the output of function is empty. Thank you!
After searching problem I can load all functions in render().
Problem was timing. I had many functions and it was not enough to execute function in render(). I just created in state chartData [] and after this updated state und used in both functions.
Code below:
class medicalDataForAll extends React.Component{
constructor(props) {
super(props)
this.state = {
chartData = {}
}
}
componentDidMount(){
this.setState({
chartData: this.getLastWeekHeartRate()
})
}
getLastWeekHeartRate() {...}
getLastWeekHeartRateX() {
let value = this.state.chartData.map((a) => a.xAxe)
return value
}
getLastWeekHeartRateY() {
let value = this.state.chartData.map((a) => a.xAxe)
return value
}
Have a nice coding!