javascript - React Native - 获取选择了哪个可触摸突出显示

标签 javascript react-native

只是试图获取按下的可触摸突出显示的当前索引。由于某种原因,它只返回数组中的最高数字。有什么想法吗?

render: function() {
    var images = [], i = 0;

    for(i=0; i<this.props.picturesList.length; i++){
        var currentIndex = i;
        images.push(
            <TouchableHighlight
                key={'c' + i}
                onPress={() => this._handleItemTouch(currentIndex)}>
                    <Image
                        style={styles.image}
                        source={{uri: this.props.picturesList[i]}} />
            </TouchableHighlight>
        );
    }
},
_handleItemTouch: function(index) {
  console.log(index);
}

最佳答案

尝试使用 let 声明 currentIndex 变量。 I 一直递增到数组中的最后一个值 + 1,let 将变量的范围限定为循环上下文中的值:

for(var i=0; i <this.props.picturesList.length; i++){
        let currentIndex = i;
        images.push(
            <TouchableHighlight
                key={'c' + i}
                onPress={() => this._handleItemTouch(currentIndex)}>
                    <Image
                        style={styles.image}
                        source={{uri: this.props.picturesList[i]}} />
            </TouchableHighlight>
        );
    }
},

我设置了一个演示 here .

关于javascript - React Native - 获取选择了哪个可触摸突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35566627/

相关文章:

javascript - 从 'expo' 导入 <MapView> 时遇到问题

ios - 世博会应用程序 'Your app is using the Advertising Identifier (IDFA)'

javascript - 存储用户敏感 API 详细信息的安全方式(本地存储还是数据库?)

javascript - 如何将 JSON 数组传递给 php 中字符串内的 javascript 函数

javascript - Angularjs JSON Unicode 字符

javascript - React/Redux 组件重新渲染

javascript - polymer 铁-ajax : How to Bind data from input element to iron-ajax's body attribute

javascript - 从外部 JS 调用函数

ios - 由于 "bundle format unrecognized, invalid, or unsuitable",Xcode 构建失败

javascript - 使用连接到容器组件转换无状态组件 - 优化