我想根据用户选择的播放器编号制作字段列表。我想做这样的事情:
generatePaymentField() {
var noGuest = this.state.guest;
var payment =
<View>
<View>
<View><Text>No</Text></View>
<View><Text>Name</Text></View>
<View><Text>Preference</Text></View>
</View>;
for (var i=0; i < noGuest; i++) {
payment = payment +
<View>
<View>
<TextInput />
</View>
<View>
<TextInput />
</View>
<View>
<TextInput />
</View>
</View>;
}
return payment;
}
render () {
var payment = this.generatePaymentField();
this.setState({payment : payment});
return (
<View>
{this.state.payment}
</View>;
)
}
但react-native认为上面的语法是指向for循环行的'意外令牌'。有没有其他方法可以实现这一目标?
答案 0 :(得分:48)
这应该有效
render(){
var payments = [];
for(let i = 0; i < noGuest; i++){
payments.push(
<View key = {i}>
<View>
<TextInput />
</View>
<View>
<TextInput />
</View>
<View>
<TextInput />
</View>
</View>
)
}
return (
<View>
<View>
<View><Text>No</Text></View>
<View><Text>Name</Text></View>
<View><Text>Preference</Text></View>
</View>
{ payments }
</View>
)
}
&#13;
答案 1 :(得分:1)
首先,我建议您多次渲染要渲染的项目(在您的案例列表中)作为单独的组件:
function Field() {
return (
<View>
<View>
<TextInput />
</View>
<View>
<TextInput />
</View>
<View>
<TextInput />
</View>
</View>
);
}
然后,在你的情况下,当基于某个数字而不是列表进行渲染时,我会在render方法之外移动for循环以获得更易读的代码:
renderFields() {
const noGuest = this.state.guest;
const fields = [];
for (let i=0; i < noGuest; i++) {
// Try avoiding the use of index as a key, it has to be unique!
fields.push(
<Field key={"guest_"+i} />
);
}
return fields;
}
render () {
return (
<View>
<View>
<View><Text>No</Text></View>
<View><Text>Name</Text></View>
<View><Text>Preference</Text></View>
</View>
{this.renderFields()}
</View>;
)
}
但是,还有很多方法可以在native native中呈现循环内容。 this article涵盖了大部分方法,如果您对更多细节感兴趣请查看!文章中的示例来自React,但一切也适用于React Native!
答案 2 :(得分:1)
render() {
var myloop = [];
for (let i = 0; i < 10; i++) {
myloop.push(
<View key={i}>
<Text>{i}</Text>
</View>
);
}
return (
<View >
<Text >Welcome to React Native!</Text>
{myloop}
</View>
);
}
}
答案 3 :(得分:0)
render() {
var myloop = [];
for (let i = 0; i < 10; i++) {
myloop.push(
<View key={i}>
<Text style={{ textAlign: 'center', marginTop: 5 }} >{i}</Text>
</View>
);
}
return (
<View >
<Text >Welcome to React Native!</Text>
{myloop}
</View>
);
}
输出 1个 2 3 4 5 6 7 8 9
答案 4 :(得分:0)
您可以创建呈现结果(付款)并使用一种奇特的方法来遍历项目,而无需添加for循环。
const noGuest = 3;
Array(noGuest).fill(noGuest).map(guest => {
console.log(guest);
});
示例:
renderPayments(noGuest) {
return Array(noGuest).fill(noGuest).map((guess, index) => {
return(
<View key={index}>
<View><TextInput /></View>
<View><TextInput /></View>
<View><TextInput /></View>
</View>
);
}
}
然后在需要的地方使用它
render() {
return(
const { guest } = this.state;
...
{this.renderPayments(guest)}
);
}
希望您有主意。
如果您想通过简单的Java脚本了解这一点,请检查Array.prototype.fill()
答案 5 :(得分:0)
renderItem(item)
{
const width = '80%';
var items = [];
for(let i = 0; i < item.count; i++){
items.push( <View style={{ padding: 10, borderBottomColor: "#f2f2f2", borderBottomWidth: 10, flexDirection: 'row' }}>
<View style={{ width }}>
<Text style={styles.name}>{item.title}</Text>
<Text style={{ color: '#818181', paddingVertical: 10 }}>{item.taskDataElements[0].description + " "}</Text>
<Text style={styles.begin}>BEGIN</Text>
</View>
<Text style={{ backgroundColor: '#fcefec', padding: 10, color: 'red', height: 40 }}>{this.msToTime(item.minTatTimestamp) <= 0 ? "NOW" : this.msToTime(item.minTatTimestamp) + "hrs"}</Text>
</View> )
}
return items;
}
render() {
return (this.renderItem(this.props.item))
}