【问题标题】:how to send param in react native如何在本机反应中发送参数
【发布时间】:2020-05-01 08:45:21
【问题描述】:

这里我进行登录,登录成功后将id参数发送到主页, 在主页上,我根据登录用户 ID 显示数据。我在登录页面发送了一个参数。

之后我把参数拿到首页,但是这里的结果是空的。 如何正确解析数据?

页面登录

 //function login 
    fetch (......)
      .then((response) => response.json())
            .then((responseJson) => {
              if(responseJson.status == 'ok'){
                this.props.navigation.navigate('home', { id_user: id_user });
              }
              else if(responseJson.status == 'failed') {
              ....
             }


    <Button block info style={styles.mainBtn} onPress={this.login}>
       <Text style={styles.btnText}>Submit</Text>
    </Button>

首页

componentDidMount(){
      const { params } = this.props.navigation.state; 
      console.log(params)

      const url = 'https://example.com/data?id_user='+params.id_user
      fetch(url)
      .then((response)=> response.json())
      .then((responseJson)=>{
        // console.log(responseJson.data)
          this.setState({
              dataSource: responseJson.data,
              isLoading : false
          })
      })
      .catch((error)=>{
          console.log(error)
      })
  }

在首页,这里我想解析正在记录的id数据,但是控制台的时候结果为null

【问题讨论】:

    标签: reactjs api react-native parameters navigation


    【解决方案1】:

    在您的Home 屏幕中,尝试在componentDidMount 生命周期方法中像这样获取id_user

    componentDidMount(){
          const id = this.props.navigation.state.params.id_user
          console.log(id)
          //...rest of the code
    }
    

    如果这不起作用,请像这样在 Login 屏幕中配置您的 navigation 语句。

    if(responseJson.status == 'ok'){
      this.props.navigation.navigate({'home', { id_user: id_user }});
    }
    

    【讨论】:

    • undifine 不是评估这个 props.route.param 的对象
    • 我已经更新了答案,你可以试试吗? @HarryWardana
    • 结果未定义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多