【问题标题】:TypeError: undefined is not an object (evaluating 'this.state.Gas') in React NativeTypeError: undefined is not an object (evalating 'this.state.Gas') in React Native
【发布时间】:2020-10-16 03:15:28
【问题描述】:

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

TypeError: undefined is not an object (evalating '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,
            });
          });
      });
  };

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

【问题讨论】:

  • 您是否将this 绑定到confirm 函数以定义this.state
  • 应该在构造函数中绑定thisthis.confirm = this.confirm.bind(this); 或将confirm 转换为箭头函数,以便自动为您绑定this
  • 问题标题为TypeError: undefined is not an object (evaluating 'this.state.Gase')...,而在描述中为TypeError: undefined is not an object (evaluating 'this.state.Gas')。请准确更新您遇到的错误,或者您是否同时遇到这两个错误?

标签: javascript reactjs react-native


【解决方案1】:

我认为你应该绑定确认功能。

onPress={this.confirm.bind}

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

【讨论】:

    【解决方案2】:

    React 处理程序不会自动绑定到它们所在的元素/类。为此,您可以使用以下代码在构造函数中绑定它们:

    this.confirm = this.confirm.bind(this)

    你也可以使用 ES6 作为另一种选择,例如:

    <Button onClick={() => this.onConfirm()}
    

    【讨论】:

      【解决方案3】:

      根据您给定的代码。在您的代码构造函数中

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

      您已经预定义了“GAS”状态,但我们没有找到“Gase”。 是错字问题还是其他?如果否,则可以将构造函数更改为

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

      然后你在 componentDidMount 或其他地方更新。 进一步你可以查看 React 的 State and Lifecycle

      【讨论】:

      • 报错TypeError: undefined is not an object (evaluating 'this.state.Gas'),所以没有提示使用Gase
      • 基本上在标题中,(Gase)已经提到了。