TypeError : undefined is not an object (evaluating 'this3.props')

TypeError : undefined is not an object (evaluating 'this3.props')

我正在尝试在我的 RN 应用程序中使用 expo 的 react-navigation。但是当我试图通过 << this.props.navigation.navigate("Detail"); >> 我的 TouchableOpacity onPress 出现了这样的错误。

TypeError : undefined is not an object (evaluating 'this3.props')

下面是我的代码(下面的代码全部在for语句里面,'count'是for语句中使用的变量)

lecture_name = lecture_name.slice(1, lecture_name.lastIndexOf('('))
const lecture_value = Value_list[count].slice(4)
all_TO.push(
          <TouchableOpacity
            style={styles.button} key={lecture_value}
            onPress={
              () => {
                // test_to_educlass(lecture_value);
                // test_toRender();
                this.props.navigation.navigate("Detail");
              }
            }
          >
            <Text> {lecture_name} </Text>
          </TouchableOpacity>
          )

经过一些搜索,我发现我无法在函数中访问 'this.props',但我没有找到我的代码的任何答案。

谁能帮帮我?

更新:在导入 useNavigation 之前的原始代码中,您能否像这样创建一个新的 onPress 函数作为回调,并在您的构造函数中添加一个 this 绑定。

constructor(props) {
    super(props);
    this.onPress = this.onPress.bind(this);
  }

 onPress() {
  this.props.navigation.navigate("Detail");
  }

<TouchableOpacity
  style={styles.button}
  key={lecture_value}
  onPress={() => this.onPress()}
>
  <Text> {lecture_name} </Text>
</TouchableOpacity>;