React native this.props.propsName抛出未定义的错误

React native this.props.propsName抛出未定义的错误,react-native,React Native,在我的react native 0.59应用程序中,当用户单击时,道具的访问方式如下: let obj = JSON.stringify({ sender_id: this.props.user.id, event_id: this.props.eventId, }); 通过传递两个道具调用此组件: this.props.navigation.navigate("Chat", {eventId: id, user: this.state.user})

在我的react native 0.59应用程序中,当用户单击时,道具的访问方式如下:

let obj = JSON.stringify({
        sender_id: this.props.user.id,
        event_id: this.props.eventId,
      });
通过传递两个道具调用此组件:

this.props.navigation.navigate("Chat", {eventId: id, user: this.state.user});
但它抛出了一个错误:

TypeError: undefined is not an object (evaluating 'this.props.user.id')
当我改变参考道具的方式时,它会起作用:

    sender_id: this.props.navigation.state.params.user.id,
    event_id: this.props.navigation.state.params.eventId,

但是使用
this.props.props\u name
访问组件的道具。哪一个是正确的

看看react导航网站

看来你应该这样做:

const { navigation } = this.props;
const eventId= navigation.getParam('eventId', 'NO-ID');
const user= navigation.getParam('user', 'some default value');

我希望它能帮助您。

第二个选项是正确的,因为您在导航上传递的参数只能通过导航状态访问。 详情如下:

sender_id: this.props.navigation.state.params.user.id, 
event_id: this.props.navigation.state.params.eventId
或者你可以使用getParam函数,你必须传递2个参数 作为字符串 参数的默认值

sender_id: this.props.navigation.getParam('user', {id: undefined}).id, 
event_id: this.props.navigation.getParam('eventId', undefined)
欲了解更多详情,请访问