【问题标题】:Error in sending data with this.props.Navigation.navigate react native使用 this.props.Navigation.navigate 发送数据时出错反应本机
【发布时间】:2019-10-23 13:09:21
【问题描述】:

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

<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 });
  };

使用getParams之后...

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.getParam('doc_id')
    );
    const json = await response.json();
    Alert.alert(CONSTANT.BaseUrl +
      "listing/get_doctor?profile_id=" + this.props.navigation.getParam('doc_id'));
    this.setState({ fetchDoctor: json });
  };

【问题讨论】:

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


    【解决方案1】:

    试试

    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 var 传递给fetchDoctorDetail 函数。

    类似:

    async componentDidMount() {
      const docId = this.props.navigation.getParam('doc_id');
      await this.fetchDoctorDetail(docId);
    }
    
    async fetchDoctorDetail = async(docId) => {
      // do all your stuff here.
    }
    

    请注意此代码未经测试

    【讨论】:

    • 您能否使用getParam() 调用更新代码示例?
    • componentDidMount() 中尝试getParam,然后使用doc_id 参数执行fetchDoctorDetail。类似于:` async componentDidMount() { const docId = this.props.navigation.getParam('doc_id');等待 fetchDoctorDetail(docId); } `
    • 请添加我不理解的完整示例代码
    • 道歉。注释中的代码示例很难编辑。我会更新答案
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      相关资源
      最近更新 更多