React Native:单击按钮时 Add/Remove 输入字段
React Native: Add/Remove input field on click of a button
React Native:Add/Remove 单击按钮时的输入字段:
当用户单击“添加”时,我想要添加一个新的输入字段。
你怎么做到的?信息保存在 Firebase 数据库中
我在react js中找到了信息,但在react native中没有
验证字段并发送到 Firebase 的函数:
constructor(props) {
super(props);
this.state = {
Cbusto: "",
Ccintura: "",
Ccadera: "",
};
}
validate() {
if (
this.state.Cbusto == "" &&
this.state.Ccintura == "" &&
this.state.Ccadera == ""
) {
alert("empty fields");
} else if (this.state.Cbusto == "") {
alert("empty fields");
} else if (this.state.Ccintura == "") {
alert("empty fields");
} else if (this.state.Ccadera == "") {
alert("empty fields");
} else {
firebase.database().ref("medidas/").push({
Cbusto: this.state.Cbusto,
Ccintura: this.state.Ccintura,
Ccadera: this.state.Ccadera,
});
alert("Medidas guardadas");
this.props.navigation.navigate("DatosCli", {
Cbusto: this.state.Cbusto,
Ccintura: this.state.Ccintura,
Ccadera: this.state.Ccadera,
id: this.props.route.params.id,
});
}
}
渲染:
render() {
return (
<ScrollView>
<View style={styles.container}>
<Text style={styles.texto}> Contorno busto/cm</Text>
<View style={styles.input}>
<TextInput
keyboardType={"numeric"}
onChangeText={(text) => this.setState({ Cbusto: text })}
></TextInput>
</View>
<Text style={styles.texto}> Contorno cintura/cm</Text>
<View style={styles.input}>
<TextInput
keyboardType={"numeric"}
onChangeText={(text) => this.setState({ Ccintura: text })}
></TextInput>
</View>
<Text style={styles.texto}> Contorno cadera/cm</Text>
<View style={styles.input}>
<TextInput
keyboardType={"numeric"}
onChangeText={(text) => this.setState({ Ccadera: text })}
></TextInput>
</View>
<View style={styles.flex}>
<View style={styles.ButtonAdd}>
<Button title="Add input" color="#B13682"></Button>
</View>
<View style={styles.ButtonDelete}>
<Button title="delete input" color="#B13682"></Button>
</View>
</View>
<View style={styles.otro}>
<Button
title="Send"
color="#B13682"
onPress={() => {
this.validate();
}}
></Button>
</View>
</View>
</ScrollView>
);
}
通过在新文件中创建组件然后导入到主视图中解决
组件代码:
render(){
return(
<View>
<Text>prueba:{this.props.item.text}</Text>
<View style={styles.input}>
<TextInput></TextInput>
</View>
</View>
)
}
然后,在概览中
导入组件:
import Campo from "./campoInput";
我们创建一个状态:
valueArray:[]
然后是按添加字段按钮调用的函数
agregarCampo = () => {
this.addNewEle = true;
const newlyAddedValue = { text: "prueba" };
this.setState({
valueArray: [...this.state.valueArray, newlyAddedValue],
});
};
最后在渲染中我们遍历 valueArray 和 return 组件
<View style={{ flex: 1, padding: 4 }}>
{this.state.valueArray.map((ele) => {
return <Campo item={ele}
onChangeText={(text) => this.setState({ info: text })}
/>;
})}
</View>
<View style={styles.ButtonAdd}>
<Button
title="Add input"
color="#B13682"
onPress={this.agregarCampo}
></Button>
</View>
React Native:Add/Remove 单击按钮时的输入字段:
当用户单击“添加”时,我想要添加一个新的输入字段。
你怎么做到的?信息保存在 Firebase 数据库中
我在react js中找到了信息,但在react native中没有
验证字段并发送到 Firebase 的函数:
constructor(props) {
super(props);
this.state = {
Cbusto: "",
Ccintura: "",
Ccadera: "",
};
}
validate() {
if (
this.state.Cbusto == "" &&
this.state.Ccintura == "" &&
this.state.Ccadera == ""
) {
alert("empty fields");
} else if (this.state.Cbusto == "") {
alert("empty fields");
} else if (this.state.Ccintura == "") {
alert("empty fields");
} else if (this.state.Ccadera == "") {
alert("empty fields");
} else {
firebase.database().ref("medidas/").push({
Cbusto: this.state.Cbusto,
Ccintura: this.state.Ccintura,
Ccadera: this.state.Ccadera,
});
alert("Medidas guardadas");
this.props.navigation.navigate("DatosCli", {
Cbusto: this.state.Cbusto,
Ccintura: this.state.Ccintura,
Ccadera: this.state.Ccadera,
id: this.props.route.params.id,
});
}
}
渲染:
render() {
return (
<ScrollView>
<View style={styles.container}>
<Text style={styles.texto}> Contorno busto/cm</Text>
<View style={styles.input}>
<TextInput
keyboardType={"numeric"}
onChangeText={(text) => this.setState({ Cbusto: text })}
></TextInput>
</View>
<Text style={styles.texto}> Contorno cintura/cm</Text>
<View style={styles.input}>
<TextInput
keyboardType={"numeric"}
onChangeText={(text) => this.setState({ Ccintura: text })}
></TextInput>
</View>
<Text style={styles.texto}> Contorno cadera/cm</Text>
<View style={styles.input}>
<TextInput
keyboardType={"numeric"}
onChangeText={(text) => this.setState({ Ccadera: text })}
></TextInput>
</View>
<View style={styles.flex}>
<View style={styles.ButtonAdd}>
<Button title="Add input" color="#B13682"></Button>
</View>
<View style={styles.ButtonDelete}>
<Button title="delete input" color="#B13682"></Button>
</View>
</View>
<View style={styles.otro}>
<Button
title="Send"
color="#B13682"
onPress={() => {
this.validate();
}}
></Button>
</View>
</View>
</ScrollView>
);
}
通过在新文件中创建组件然后导入到主视图中解决
组件代码:
render(){
return(
<View>
<Text>prueba:{this.props.item.text}</Text>
<View style={styles.input}>
<TextInput></TextInput>
</View>
</View>
)
}
然后,在概览中
导入组件:
import Campo from "./campoInput";
我们创建一个状态:
valueArray:[]
然后是按添加字段按钮调用的函数
agregarCampo = () => {
this.addNewEle = true;
const newlyAddedValue = { text: "prueba" };
this.setState({
valueArray: [...this.state.valueArray, newlyAddedValue],
});
};
最后在渲染中我们遍历 valueArray 和 return 组件
<View style={{ flex: 1, padding: 4 }}>
{this.state.valueArray.map((ele) => {
return <Campo item={ele}
onChangeText={(text) => this.setState({ info: text })}
/>;
})}
</View>
<View style={styles.ButtonAdd}>
<Button
title="Add input"
color="#B13682"
onPress={this.agregarCampo}
></Button>
</View>