gpt4 book ai didi

javascript - 类型错误 : undefined is not an object (evaluating 'this.state.Gas' ) in React Native

转载 作者:行者123 更新时间:2023-12-03 07:07:51 25 4
gpt4 key购买 nike

在 React Native 中开发应用程序 我尝试使用客户端数据和产品类型发出动态警报,但是当我尝试构建连接字符串的变量时,出现此错误。

TypeError: undefined is not an object (evaluating 'this.state.Gas')

这是我的代码的相关部分:

渲染:

render() {
return (
<ScrollView style={styles.container}>
<Text style={styles.Titulo}>Pide tu cilindro!</Text>
<View style={styles.cuadros}>
<View style={styles.gas}>
<Image
source={require("./assets/licuado-2kg.png")}
style={{ height: 170, width: 170 }}
/>
</View>
<View style={styles.texto}>
<Text style={styles.cilindros}>GASCO 2 kg</Text>
<TouchableOpacity
style={styles.btnpedir}
onPress={this.confirm}
>
<Text style={styles.pedir}>Pedir</Text>
</TouchableOpacity>
</View>
</View>

</ScrollView>
);
}
}

构造函数:

constructor(props) {
super(props);
this.state = {
email: "",
displayName: "",
Ciudad: "",
Comuna: "",
Direccion: "",
Gas: "GASCO 2 kg"
};
}

功能:

confirm() {
let variable = this.state.Gas
let datos = this.state.displayName + this.state.Ciudad + this.state.Comuna + this.state.Direccion
let texto = "Deseas confirmar el pedido de: ".concat(variable, " a: ", datos)
Alert.alert(
"Confirmar pedido",
texto,
[
{
text: "Confirmar",
onPress: () => console.log("Confirmar"),
},
{
text: "Editar datos",
onPress: () => console.log("Editar"),
},
{
text: "Cancelar",
onPress: () => console.log("Cancelar"),
style: "cancel",
}
],
{ cancelable: false }
);
}

ComponentDidMount:

componentDidMount() {
const { email, displayName } = firebase.auth().currentUser;
this.setState({ email, displayName });

firebase
.firestore()
.collection("Users")
.where("Email", "==", email)
.get()
.then((snapshot) => {
this.setState({ Doc: snapshot.docs[0].id });
firebase
.firestore()
.collection("Users")
.doc(this.state.Doc)
.get()
.then((doc) => {
this.setState({
Ciudad: doc.data().City,
Direccion: doc.data().Direction,
Comuna: doc.data().Comuna,
});
});
});
};

我该如何解决这个问题??

最佳答案

我认为你应该绑定(bind)确认函数。

onPress={this.confirm.bind}

https://reactjs.org/docs/faq-functions.html#how-do-i-bind-a-function-to-a-component-instance

关于javascript - 类型错误 : undefined is not an object (evaluating 'this.state.Gas' ) in React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64382434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com