TypeError:undefined不是React Native中的一个对象(评估'this.state.Gas')

mxmlnlrcn

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

TypeError:undefined不是对象(评估“ 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,
            });
          });
      });
  };

我该如何解决呢?

车虎

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

onPress={this.confirm.bind}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:undefined不是React Native中的一个对象(评估'this.state.Gase')

React Native得到TypeError:undefined不是一个对象(正在评估'_this.state.apiData.items.map')

TypeError:undefined不是一个对象(评估“ navigation.state”)-反应本机

React Native:null不是一个对象(评估'_this.state.displayErrors')

TypeError:undefined不是带有React-router-dom的对象(评估“ undefined.state”)

React Native Undefined 不是对象(评估“this.state.input”)

“ null 不是一个对象(评估'this.state.array')“

React Native:TypeError:undefined不是一个对象(评估'_this.props.data.map')

TypeError:undefined不是一个对象(正在评估“ navigation.push”)React Native

React Native:TypeError:undefined不是一个对象(评估'this.props.navigation.navigate')

React Native TypeError:undefined 不是一个对象(评估'_ref.item')

undefined 不是一个对象(评估 'routes[state.index].key')

undefined 不是一个对象(评估'this.state.product.images.map')

undefined 不是一个对象(评估'this.state.markers.map')渲染

React Native,TypeError:undefined不是一个对象,当它是

TypeError:undefined不是对象(评估'_this3.state.bind')

react-native-maps-TypeError:未定义不是对象(正在评估'_this.state.region')

React Native:'undefined 不是一个对象(评估'_react3.PropTypes.bool')

react-native:undefined中的抽屉导航错误不是一个对象(评估'_this.props.navigation.openDrawer()')

React Native:TypeError:this.state.schedule.map不是对象

React Native:undefined 不是一个对象(评估'_this2.props.navigation.navigate')

React Native undefined不是一个对象(评估“ Mapbox.GL.UserTrackingModes”)

undefined 不是一个对象(评估 'Context._context') - React Native

React Native undefined不是一个对象(评估“ props.navigation.toggleDrawer”)

Undefined 不是一个评估 title.length React Native 的对象

Undefined 不是一个对象(评估'this.props.navigation.navigate')React native

undefined 不是一个对象(评估'ToolbarAndroid.propTypes')react-native-general-calendars

错误 react-native-video : undefined 不是一个对象(评估 'RCTVideoInstance.Constants')

undefined 不是一个对象(评估 'this.onbuffer') React Native