反应本土键无法正常更新的for循环? [英] React native key not updating properly in for-loop?

查看:110
本文介绍了反应本土键无法正常更新的for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的阵营,本机应用程序的函数中,我遍历数组键和拉离火力地堡各自的裁判。

不过,在我的for循环,虽然适当的裁判始终评估,for循环自身内部的核心价值始终是3,因此 totalItemsCompleted (这是等于键的值在长度 userItemsArray )总是等于3。

  listenForItems(itemsRef){
    VAR userItemsArray = {
        0:[1],
        1:[5,6]
        2:[8,9,10],
        3:[11,12,13]
    };    变种pastItems = [];
    变种currentItems = [];    对(在userItemsArray VAR键){
        的console.log(KEY1+键); //关键是这里正确
        VAR的itemref = itemsRef.child(键);
        itemRef.on('值',(SNAP)=> {
            VAR totalItemsOnList = snap.val()items.length。
            VAR totalItemsCompleted = userItemsArray [关键]。长度;
            的console.log(KEY2+键); //钥匙总是在这里3
            如果(totalItemsOnList === totalItemsCompleted){
                pastItems.push({
                    标题:snap.val()称号,
            });
            }
            其他{
                currentItems.push({
                    标题:snap.val()称号,
                });
            }            this.setState({
                currentItems:this.state.currentItems.cloneWithRows(currentItems)
                pastItems:this.state.pastItems.cloneWithRows(pastItems)
            });
        });
    }
}


解决方案

这里的问题是火力的异步特性。您的循环不会等待火力调用来完成,因此不断更新键值。

使用关键变量,你应该用你的火力点快照这样关键的欧洲工商管理学院:

  VAR的itemref = itemsRef.child(键);
    itemRef.on('值',(SNAP)=> {
        VAR totalItemsOnList = snap.val()items.length。
        。变种totalItemsCompleted = userItemsArray [snap.key()]的长度;
        的console.log(KEY2+ snap.key());

I have a function in my React-native app in which I loop through keys in an array and pull the respective ref from Firebase.

However, within my for loop, although the proper ref is always evaluated, the key value inside the for-loop itself is always 3, and therefore totalItemsCompleted (which is equal to the length of the key's value in userItemsArray) is always equal to 3.

 listenForItems(itemsRef) {
    var userItemsArray = {
        0: [1],
        1: [5, 6],
        2: [8, 9, 10],
        3: [11, 12, 13]
    };

    var pastItems = [];
    var currentItems = [];

    for (var key in userItemsArray) {
        console.log("KEY1 " + key); //key is correct here
        var itemRef = itemsRef.child(key);
        itemRef.on('value', (snap) => {
            var totalItemsOnList = snap.val().items.length;
            var totalItemsCompleted = userItemsArray[key].length;
            console.log("KEY2 " + key); //key is always 3 here
            if (totalItemsOnList===totalItemsCompleted) {
                pastItems.push({
                    title: snap.val().title,
            });
            }
            else {
                currentItems.push({
                    title: snap.val().title,
                });
            }

            this.setState({
                currentItems: this.state.currentItems.cloneWithRows(currentItems),
                pastItems: this.state.pastItems.cloneWithRows(pastItems)
            });
        });
    }
}

解决方案

The problem here is the asynchronous nature of firebase. Your for loop isn't waiting for the firebase call to finish so it keeps updating the key value.

Insead of using the key variable you should use the key of your firebase snapshot like this:

var itemRef = itemsRef.child(key);
    itemRef.on('value', (snap) => {
        var totalItemsOnList = snap.val().items.length;
        var totalItemsCompleted = userItemsArray[snap.key()].length;
        console.log("KEY2 " + snap.key()); 

这篇关于反应本土键无法正常更新的for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆