score:6

Accepted answer

Use let instead of var here:

Change

var myId = this.state.siteid[i].id;
var myDaily = this.state.siteid[i].daily;
// ....

to

let myId = this.state.siteid[i].id;
let myDaily = this.state.siteid[i].daily;
// ....

var is scoped to the nearest function and not to the while block. It gets hoisted, and your code will be something like:

var i;
var myId;
var myDaily;

i = 0;

while ( i < 2 ) {
    myId = this.state.siteid[i].id;
    myDaily = this.state.siteid[i].daily;
}

Since dataProvider calls are asynchronous, the value of myId will be replaced with AK values by the first time call is done.

dataProvider(GET_LIST, 'clicks', {
    //
})
 .then(response => this.setState({ [myWeekly]: response.data.count }))
       /* ^^ This callback runs after the while block
           By this time, myDaily === "AKdaily" */

Related Query

More Query from same tag