没有传递给组件的道具反应原生

时间:2021-05-25 23:55:08

标签: javascript react-native

我们有一个组件,通知单元格,如果您单击通知单元格,它会打开一个包含帖子详细信息的屏幕。但是,通知单元不会将道具传递到帖子详细信息屏幕。

通知单元:

showThoughtsPage = async() => {
  await firebase.firestore()
    .collection('thoughts')
    .doc(this.state.postID)
    .get()
    .then((doc) => {
      if (doc.exists) {
        console.log(doc.data().username) //<--- works

        this.state.navigation.push('ThoughtsDetails',
        {
          Tusername: doc.data().username,
          Tdescription: doc.data().description,
          Timage: doc.data().image,
          Tdate_created: doc.data().date_created,
          TlikesCount: doc.data().likesCount,
          TcommentsCount: doc.data().commentsCount,
          TviewsCount: doc.data().viewsCount,
          Tcategory: doc.data().category,
          TpostID: doc.data().postID,
          Tuid: doc.data().uid,
          Tlink: doc.data().link,
          TmediaType: doc.data().mediaType,
        })
      }
    });
}

帖子详细信息屏幕:

constructor(props) {
 super(props);
 this.state = {
   username: this.props.Tusername,
   image: this.props.Timage,
   description: this.props.Tdescription,
   postID: this.props.TpostID,
   link: this.props.Tlink,
   navigation: this.props.navigation,
   date_created: this.props.Tdate_created,
   viewsCount: this.props.TviewsCount,
   isLoading: true,
   currentUser: firebase.auth().currentUser.uid,
   posterUID: this.props.Tuid,
   mediaType: this.props.TmediaType,
   modalOpen: false,
   commentsArray: [],
   currentViewsCount: 0,
};
console.log(this.props.Tusername + " username") // <--- undefined

}

知道我们在哪里犯了错误吗?

1 个答案:

答案 0 :(得分:0)

想通了 - 我需要像这样访问帖子详细信息中的道具:

  username: this.props.route.params.Tusername,
  image: this.props.route.params.Timage,
  description: this.props.route.params.Tdescription,
  postID: this.props.route.params.TpostID,
  link: this.props.route.params.Tlink,
  navigation: this.props.route.params.navigation,
  date_created: this.props.route.params.Tdate_created,
  viewsCount: this.props.route.params.TviewsCount,
  isLoading: true,
  currentUser: firebase.auth().currentUser.uid,
  posterUID: this.props.route.params.Tuid,
  mediaType: this.props.route.params.TmediaType,
  modalOpen: false,
  commentsArray: [],
  currentViewsCount: 0,
相关问题