使用this.props.Navigation.navigate反应发送数据时出错

时间:2019-10-23 13:09:21

标签: reactjs react-native react-native-flatlist

我正在导航中发送用户ID,但是我无法在详细信息页面上获取ID,这是我的代码...

SearchId

在以上代码中,我正在发送List<Search> distinctSearch = db.Search .GroupBy(s => s.SearchId) .Select(g => g.FirstOrDefault()) .ToList(); ,但无法在<FlatList style={{ paddingLeft: 5 }} data={this.state.TopRatedData} ListEmptyComponent={this._listEmptyComponent} keyExtractor={(x, i) => i.toString()} renderItem={({ item }) => <TouchableOpacity activeOpacity={0.9} onPress = { () => this.props.navigation.navigate("DetailDoctorscreen", {doc_id: item.ID}) }> <TopRatedCard profileImage={{ uri: `${item.image}` }} specialities={`${entities.decode(item.specialities.name)}`} name={`${entities.decode(item.name)}`} sub_heading={`${entities.decode(item.sub_heading)}`} total_rating={`${entities.decode(item.total_rating)}`} average_rating={`${entities.decode(item.average_rating)}`} /> </TouchableOpacity> 上获取它。这是代码...

doc_id

使用DetailDoctorscreen后...

fetchDoctorDetail = async () => {
    const { params } = this.props.navigation.state;
    console.log(this.props.navigation.state.doc_id);
    const response = await fetch(
      CONSTANT.BaseUrl +
      "listing/get_doctor?profile_id=" + this.props.navigation.state.doc_id
    );
    const json = await response.json();
    Alert.alert(CONSTANT.BaseUrl +
      "listing/get_doctor?profile_id=" + this.props.navigation.state.doc_id);
    this.setState({ fetchDoctor: json });
  };

2 个答案:

答案 0 :(得分:0)

尝试

this.props.navigation.getParam('doc_id');

编辑:前往此处获取更多信息:https://reactnavigation.org/docs/en/navigation-prop.html#getparam-get-a-specific-param-value-with-a-fallback

进一步澄清:

componentDidMount中获取参数,然后将docId变量传递到fetchDoctorDetail函数中。

类似的东西:

async componentDidMount() {
  const docId = this.props.navigation.getParam('doc_id');
  await this.fetchDoctorDetail(docId);
}

async fetchDoctorDetail = async(docId) => {
  // do all your stuff here.
}

请注意,此代码未经测试

答案 1 :(得分:0)

您正在从导航的状态对象而不是params对象访问param变量。请尝试以下操作:

const { params } = this.props.navigation.state;
console.log('doc id =',params.doc_id);