簡體   English   中英

TypeError: undefined is not an object(評估“this3.props”)

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

我試圖在我的 RN 應用程序中使用 react-navigation 和 expo。 但是當我試圖通過 << this.props.navigation.navigate("Detail"); >> 到我的 TouchableOpacity onPress,它給了我這樣的錯誤。

TypeError: undefined is not an object(評估“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>
          )

經過一番搜索,我發現我無法訪問 function 中的“this.props”,但我的代碼沒有找到任何答案。

任何人都可以幫助我嗎?

更新:在導入useNavigation之前的原始代碼中,您可以創建一個新的onPress function 作為這樣的回調,並在您的構造函數中添加一個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>;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM