【发布时间】:2020-06-29 06:46:03
【问题描述】:
我在渲染块中运行了 forEach,它在 控制台,但输出中不显示文本标签。
有什么问题?
import React from "react";
import { StyleSheet, Text, View } from "react-native";
class Lotto extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 6,
maxNum: 45
};
this.lottoSet = this.createLottoNumber();
}
createLottoNumber() {
let lottoSet = new Set();
let rNum;
for (let i = 0; i < this.state.count; i++) {
rNum = Math.round(Math.random() * (this.state.maxNum * 1) + 1);
if (lottoSet.has(rNum)) i--;
else lottoSet.add(rNum);
}
return lottoSet;
}
render() {
return (
<View style={styles.container}>
{this.lottoSet.forEach(n => {
console.log(`<Text style={styles.item}>${n.toString()}</Text>`);
return <Text style={styles.item}>{n.toString()}</Text>;
})}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#333",
flexDirection: "row",
paddingTop: "10%",
justifyContent: "center"
},
item: {
color: "#fff",
textAlign: "center",
width: "100px"
}
});
export default Lotto;
【问题讨论】:
-
看起来像 stackoverflow.com/questions/47442462/… 的副本。别担心,这是人们常犯的错误。包括我 5 年前,你可以在 2015 年查看我的问题。