我是本机新手,我陷入了一个问题。 我有注册页面,其中有两个部分,第一个是父母注册,第二个是学生注册。 因此,家长可以根据那里的要求添加任何儿童注册表。现在,如果父母要删除任何子窗体或块,则具有删除信息功能,但是当我在foreach循环中绑定删除按钮单击事件时,所有按钮的参数值保持不变。因此,每次我单击删除按钮时,它都会删除最后一个表格。
这是我的代码
export default class SignUp extends React.Component {
constructor() {
super()
{
}
this.index = 1;
this.animatedValue = new Animated.Value(0);
this.state = {
username: '', password: '', email: '', phone_number: '', registrationData: [],
valueArray: [], key: 0, index: 0
}
}
AddMore = async () => {
try {
var data = this.state.index + 1;
this.setState({ index: data });
}
catch (error) {
alert(error);
}
try {
this.animatedValue.setValue(0);
let newlyAddedValue = { index: this.index }
this.setState({ disabled: true, valueArray: [...this.state.valueArray, newlyAddedValue] }, () => {
Animated.timing(
this.animatedValue,
{
toValue: 1,
duration: 500,
useNativeDriver: true
}
).start(() => {
this.index = this.index + 1;
this.setState({ disabled: false });
});
});
}
catch (error) {
alert("Error " + error);
}
}
static navigationOptions = {
title: 'Registration',
}
onRemoveBlock = (key) => {
try {
var r = this.state.valueArray;
var d = r.length - key;
r.splice(d, 1);
this.setState({ valueArray: r });
}
catch (error) {
alert(error)
}
}
render() {
let data = [{
value: 'Male',
}, {
value: 'Female'
}];
this.position = 0;
let newArray = this.state.valueArray.map((item, key) => {
this.position = key + 1;
return (
<View style={styles.container} key={key} >
<ScrollView>
<Button
title={'Remove'}
onPress={() => this.onRemoveBlock(this.position)}
/>
<TextInput
style={styles.input}
placeholder={'First Name'}
autoCapitalize="none"
placeholderTextColor='white'
onChangeText={val => this.onChangeText('childfirstname' + this.position, val)}
/>
</ScrollView>
</View>
);
});
return (
<View style={styles.container}>
<ScrollView>
<Text h1>Parent Registration</Text>
<TextInput
style={styles.input}
placeholder='First Name'
autoCapitalize="none"
placeholderTextColor='white'
onChangeText={val => this.onChangeText('firstname', val)}
/>
<Text h1>Student Registration</Text>
<View style={styles.container} key={123} >
<ScrollView>
<TextInput
style={styles.input}
placeholder={'First Name'}
autoCapitalize="none"
placeholderTextColor='white'
onChangeText={val => this.onChangeText('childfirstname0', val)}
/>
</ScrollView>
</View>
<View style={{ flex: 1, padding: 4 }}>
{
newArray
}
</View>
<Text>{"\n"}</Text>
</ScrollView>
<TouchableOpacity activeOpacity={0.8} style={styles.btn} disabled={this.state.disabled} onPress={this.AddMore}>
<Image source={require('./assets/add.png')} style={styles.btnImage} />
</TouchableOpacity>
</View>
)
}
}
答案 0 :(得分:0)
当我单击删除按钮时,由于键的原因,文本输入的值已用其他文本字段更新。它删除了我要删除的部分,因此一切正常,我只需要正确地控制索引即可。
我已经更新了代码,现在可以正常工作
我只需要将索引值而不是键分配给valuearray循环中的位置
let newArray = this.state.valueArray.map((item, key) => {
//this.position = key + 1;
this.position=item.index;
return (
<View style={styles.container} key={this.position} >
<ScrollView>
<Button
title={'Actual Key -> '+item.index +' key -> '+key}
onPress={() => this.onRemoveBlock(key)}
key={key}
/>
<TextInput
style={styles.input}
placeholder={'First Name'+this.position}
autoCapitalize="none"
placeholderTextColor='white'
onChangeText={val => this.onChangeText('childfirstname' + this.position, val)}
/>
</ScrollView>
</View>
);
});
所以现在控制值将始终保持不变