未定义不是组件上的对象(评估“ this.props.navigation.navigate”)

时间:2019-08-08 22:16:33

标签: javascript react-native react-navigation

在post组件内设置一个简单的注释组件后!点击回复后,我很想重定向到回复组件!

但是,导航仍然会弹出这些错误提示:

  

TypeError:TypeError:未定义不是对象(正在评估   'this.props.navigation.navigate')

  

未定义不是对象(正在评估   '_this2.props.navigation.navigate')onPress

这似乎是一个非常普遍的问题! 我看到的所有答案都无法真正简化和解释这种情况的发生! 因此,请任何人澄清为什么会发生这种情况?

正在发生的代码是这样的:

import React, { Component } from "react";
import { View, Text } from "react-native";

import styles from "../assets/styles";
import Comments from "./Comments";

import ReplyScreen from "./ReplyScreen";
class Comment extends Component {
  constructor(props) {
    super(props);

    this.state = {
      visible: false
    };


  }



  submitReply = (comment, parentId) => {
    this.props.onSubmitReply(comment, parentId);
  };

  render() {
    const comment = this.props.comment;
    const { navigation } = this.props;
    return (
      <View>
        <Text style={styles.body}>{comment.comment} </Text>
        <Text
          style={{ color: "blue" }}
          onPress={() => navigation.navigate("ReplyScreen")}

          //  onPress={() => {
          /* 1. Navigate to the Details route with params */
          //  navigate("Reply", {
          //  comment: comment.id
          //key: comment.id
          //});
          //  }}
        >
          Reply
        </Text>



        <Comments comments={comment.replies} onSubmitReply={this.submitReply} />
      </View>
    );
  }
}

export default Comment;

1 个答案:

答案 0 :(得分:0)

也许我可以用一些代码来解释它:

var my_object = {
    "foo": "lorem ipsum"
};
console.log(my_object.foo); // "lorem ipsum"
console.log(my_object.bar); // undefined
console.log(my_object.bar.foo); // Error!

var my_uninitialized_object;
console.log(my_uninitialized_object); // undefined
console.log(my_uninitialized_object.foo); // Error!

这有意义吗?