React-Native:undefined 不是一个对象(评估 'this.props = props')

React-Native: undefined is not an object (evaluating 'this.props = props')

我是 react-native 的新手,我正在尝试将 dialogflow 与 react-native 集成。

我的代码是:

import React, {Component} from 'react';
import { StyleSheet, View, Button } from 'react-native';
import { Dialogflow_V2 } from "react-native-dialogflow";

class App extends Component() {
  constructor(props) {
    super(props);

    Dialogflow_V2.setConfiguration(
        "sample.iam.gserviceaccount.com",
        '-----BEGIN PRIVATE KEY-----\sample\n-----END PRIVATE KEY-----\n"',
        Dialogflow_V2.LANG_ENGLISH,
        'sample-44ce3'
    );
  }
  render() {
    const { dialogflow } = this.props.Dialogflow_V2;
    return (
      <View style={styles.container}>
        <Button onPress={() => {
            dialogflow.requestQuery("hello", result=>console.log(result), error=>console.log(error));
          }}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default App;

但是它向我显示了这个错误

谁能解释一下这到底是什么问题,需要什么修正? 提前致谢。

您没有在应用程序组件中收到任何 Dialogflow_V2 道具,因此显示错误。只需替换以下行,我认为这行得通

 const { dialogflow } = this.Dialogflow_V2;

那是因为它无法从 props 中找到 Dialogflow_V2 引用。

删除渲染函数中的这一行

const { dialogflow } = this.props.Dialogflow_V2;

直接使用已经导入的Dialogflow_V2

Dialogflow_V2.requestQuery("hello", result=>console.log(result), error=>console.log(error));

改变这个:

class App extends Component() {}

为此:

class App extends Component {}