我想显示一个项目列表,当我单击每个项目时,它会提醒我它的价值。
_onPressButton(i) {
Alert.alert('You tapped the button number ' + i)
}
_renderBlockLebel = (number) => {
var items = [];
for (var i=0; i < number; i++) {
items.push(
<TouchableOpacity onPress={() => this._onPressButton(i)} key={i}>
<View style={styles.boxItem}>
<Text>Level {i}</Text>
</View>
</TouchableOpacity>
);
}
return items;
}
render(){
return(
<View style={styles.box}>{this._renderBlockLebel(18)}</View>
);
}
我有18个项目,它显示的很好,但是当我单击任何项目时,它会提示“您点击了按钮号18”,我传递的值没有改变。
我的代码有什么问题?我是React Native的新手。
答案 0 :(得分:2)
按下标签时,i
的值已达到number
(您的情况是18)。您需要在每次迭代中保留i
的值。将_onPressButton
声明更改为
_onPressButton = (i) => () => {
Alert.alert('You tapped the button number ' + i)
}
还将onPress
处理程序更改为
<TouchableOpacity onPress={this._onPressButton(i)} key={i}>
看看closure。
希望这会有所帮助!
答案 1 :(得分:1)
我试图考虑一个变量并且它起作用。
[ymu ys2 fmu fs2 ] = gp(hyp, inf, mean, cov, lik, x, y, xs)
ymu column vector (of length ns) of predictive output means
ys2 column vector (of length ns) of predictive output variances
fmu column vector (of length ns) of predictive latent means
fs2 column vector (of length ns) of predictive latent variances
lp column vector (of length ns) of log predictive probabilities