我是React Native
中的新成員。在React Native中按下按鈕時,一個接一個地追加TextInput組件
我使用兩個按鈕在視圖中添加TextInput
組件。 當我按下Add
按鈕時,它會將一個TextInput
組件推入視圖中,當我按下Add Again
按鈕時,它會將另一個TextInput
組件推向下一個組件。
但是當我再次按下Add
按鈕時,它將TextInput
組件推到第一個按鈕下面。但我想把它推到最後一個之下。
像:Add |Add | Add again.
但我需要Add | Add Again | Add
等。
可以做些什麼來實現這一目標?我跟隨this。
'use strict';
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
TextInput,
} from 'react-native';
let index = 0
class Test extends Component{
constructor(){
super();
this.state = {
arr: []
};
}
insertSomeThing(){
this.state.arr.push(index++)
this.setState({ arr: this.state.arr })
}
render() {
let arr = this.state.arr.map((r, i) => {
return (
<View key={ i } style={styles.insertStyle}>
<TextInput placeholder = 'abc' />
</View>
);
})
let arrAnother = this.state.arr.map((r, i) => {
return (
<View key={ i } style={styles.insertStyle}>
<TextInput placeholder = 'def' />
</View>
);
})
return (
<View style={styles.container}>
<View>
<TouchableHighlight
onPress={ this.insertSomeThing.bind(this) }
style={styles.button}>
<Text>Add</Text>
</TouchableHighlight>
</View>
<View>
<TouchableHighlight
onPress={ this.insertSomeThing.bind(this) }
style={styles.button}>
<Text>Add Again</Text>
</TouchableHighlight>
</View>
{ arr }
{arrAnother}
</View>
);
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 60,
},
insertStyle: {
height:40,
alignItems: 'center',
justifyContent: 'center',
borderBottomColor: '#ededed',
borderBottomWidth: 1
},
button: {
alignItems: 'center',
justifyContent: 'center',
height:55,
backgroundColor: '#ededed',
marginBottom:10
}
});
AppRegistry.registerComponent('Test',() => Test);
如何使用onChangeText和值,以便我可以單獨檢索輸入文本? –
查看我的編輯 – leo7r
最後一個,我需要在映射函數中更改哪些地方,或者如果我想推入如下組件:TextInput |文字| TextInput等? –