React setState在while循环中未设置状态

时间:2019-03-05 04:29:45

标签: javascript reactjs react-native setstate react-admin

我想避免数据乘法,因此我想创建一个循环,以针对不同的site_id调用我的dataprovider。

我创建了一个while循环,并在此while循环中设置状态值。我意识到从我的2个元素数组(我有2个站点)中,只有1个处于状态,而另一个没有。

class Dashboard extends Component {
state = {
    username: localStorage.getItem('username'),
    siteid: [{
        id: 1,
        daily: "EKdaily",
        weekly: "EKweekly",
        monthly: "EKmonthly",
        total: "EKtotal",
        },
    {
        id: 2,
        daily: "AKdaily",
        weekly: "AKweekly",
        monthly: "AKmonthly",
        total: "AKtotal",
    }]

};

componentDidMount() {
    dataProviderFactory('rest').then(
        dataProvider => {
            dataProvider(GET_ONE, 'users', {
                id: this.state.username,
            })
                .then(response => this.setState({ fullname: response.data.fullname }))

            dataProvider(GET_LIST, 'sites', {
                filter: { q: '' },
                sort: { field: '', order: '' },
                pagination: { page: 1, perPage: 1 },
            })
                .then(response => this.setState({ sites: response.data }))

            var i = 0;
            while ( i < 2 ) {
                var myId = this.state.siteid[i].id;
                var myDaily = this.state.siteid[i].daily;
                var myWeekly = this.state.siteid[i].weekly;
                var myMonthly = this.state.siteid[i].monthly;
                var myTotal = this.state.siteid[i].total;
                console.log("id", myId, myDaily, myWeekly, myMonthly, myTotal);
             dataProvider(GET_LIST, 'clicks', {
                filter: { interval: 'day', site_id: myId, count: '1', },
                sort: { field: 'date', order: 'DESC' },
                pagination: { page: 1, perPage: 50 },
            })
                .then(response => this.setState({ [myDaily]: response.data.count }))
            dataProvider(GET_LIST, 'clicks', {
                filter: { interval: 'week', site_id: myId, count: '1', },
                sort: { field: 'date', order: 'DESC' },
                pagination: { page: 1, perPage: 50 },
            })
                .then(response => this.setState({ [myWeekly]: response.data.count }))
            dataProvider(GET_LIST, 'clicks', {
                filter: { interval: 'month', site_id: myId, count: '1', },
                sort: { field: 'date', order: 'DESC' },
                pagination: { page: 1, perPage: 50 },
            })
                .then(response => this.setState({ [myMonthly]: response.data.count }))
            dataProvider(GET_LIST, 'clicks', {
                filter: { interval: 'all', site_id: myId, count: '1', },
                sort: { field: 'date', order: 'DESC' },
                pagination: { page: 1, perPage: 50 },
            })
                .then(response => this.setState({ [myTotal]: response.data.count }))
                i++;
            }
        }
    );
}

render() {
    const {
        fullname,
        username,
        EKdaily,
        EKweekly,
        EKmonthly,
        EKtotal,
        AKdaily,
        AKweekly,
        AKmonthly,
        AKtotal,
    } = this.state;
    const myPageHeader = `Udvozlunk az Admin feluleten ${fullname}!`;
    return (
        <div style={styles.flex}>
            <div style={styles.leftCol}>
                <div>
                    <Card>
                        <CardHeader title={myPageHeader} />
                        <CardContent>Bejelentkezett felhasznalo: {username}</CardContent>
                    </Card>
                </div>
                <div stlye={ { magrinTop: 200} }>
                    <Typography color="textPrimary">123.hu kattintas statisztikak</Typography>
                </div>
                <div style={styles.flex}>
                    <DisplayClick value={EKdaily} title="Napi Kattintas" />
                    <DisplayClick value={EKweekly} title="Heti Kattintas" />
                    <DisplayClick value={EKmonthly} title="Havi Kattintas" />
                    <DisplayClick value={EKtotal} title="Ossz Kattintas" />
                </div>
                 <div stlye={ { magrinTop: 20} }>
                    <Typography color="textPrimary">345.hu kattintas statisztikak</Typography>
                </div>
                <div style={styles.flex}>
                    <DisplayClick value={AKdaily} title="Napi Kattintas" />
                    <DisplayClick value={AKweekly} title="Heti Kattintas" />
                    <DisplayClick value={AKmonthly} title="Havi Kattintas" />
                    <DisplayClick value={AKtotal} title="Ossz Kattintas" />
                </div>
            </div>
        </div>
   );
}
}

在此状态下仅设置AK *,而未设置EK *。

我该怎么办?

1 个答案:

答案 0 :(得分:5)

在此处使用let代替def convert(str): new_str =str[0] quote=False for c in str[1:]: if c in ["'", '"']: if quote==False: new_str=new_str+',' quote=not quote if c not in ["'", '"'] and quote==False: new_str = new_str + ','+c else: new_str = new_str + c return new_str

更改

var

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

let myId = this.state.siteid[i].id; let myDaily = this.state.siteid[i].daily; // .... 的作用域是最接近的函数,而不是var块。它被吊起,您的代码将类似于:

while

由于var i; var myId; var myDaily; i = 0; while ( i < 2 ) { myId = this.state.siteid[i].id; myDaily = this.state.siteid[i].daily; } 调用是异步的,因此在第一次调用时dataProvider的值将被myId值替换。

AK