我正在尝试打印从 1 到 10 的数字,以便用户可以按其中任何一个。 但是,我收到此错误,内容为
Invariant Violation: Text strings must be rendered within a
<Text>
component.
这是 render 中的代码。
render(){
let arr=[];
for(let index=1;index<=this.state.max_rating;index++){
arr.push(
<TouchableOpacity
activeOpacity={0.7}
key={index}
onPress={this.UpdateRating.bind(this, index)}>
<Text key={index}>
{'lol'}
</Text>
>
</TouchableOpacity>
);
}
return(
<View style={styles.MainContainer}>
<Text> The code is working man </Text>
<View style={styles.childView}>{arr}</View>
</View>
);
}
最佳答案
您的代码中存在轻微的语法问题
for(let index=1;index<=this.state.max_rating;index++){
arr.push(
<TouchableOpacity
activeOpacity={0.7}
key={index}
onPress={this.UpdateRating.bind(this, index)}>
<Text key={index}>
{'lol'}
</Text>
> <-- extra character which is treated as a string giving you the warning
</TouchableOpacity>
);
}
删除 >
在 <TouchableOpacity>
内呈现的字符组件,它将正常工作
关于javascript - 不变违规 : Text strings must be rendered within a component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54345550/