【问题标题】:how to use getparam from navigation properly in react native?如何在本机反应中正确使用导航中的getparam?
【发布时间】:2020-05-30 20:47:03
【问题描述】:

我正在使用反应导航 v4 我使用navigation.navigate 在我构建的其他页面中发送参数, 当我尝试在其他页面中执行相同操作时,我得到未定义的值。

我想通过那个

let VolunteerDest = documentSnapshot.data().delegation;
console.log('testing user delegation: ', VolunteerDest)  // <~~~~~~~~~ its working ~~~~~~~~~~
this.props.navigation.navigate('Volunteer', {userDelegation: VolunteerDest});

在另一页上,我在 compDidMount / Constructor 中尝试了它。它们都没有工作:

 const id = this.props.navigation.getParam('userDelegation');
 console.log('testing user delegation:' , id);  // <~~~~~~~~~~~~~~  undefined

有人可以告诉我它有什么问题吗? 我阅读了导航文档并在这里和谷歌搜索,但找不到我的错误。(就像我说它适用于不同的页面导航但在这里我遇到了这个问题)

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    不用担心,也可以像这样获取state中的值

    this.state = {
            subCatName: this.props.navigation.state.params.userDelegation
        };
    

    【讨论】:

    • tnx 为您提供答案,它给了我:TypeError: Cannot read property 'subCatName' of undefined. 堆栈导航器可能有问题?但如果它把我带到那个页面,我认为它也应该传递值
    • 嘿,这是我的错误,实际上您不需要将 subCatName 放在您需要调用关键字的参数之后。用户委托
    【解决方案2】:
        First Scrren :
    
         <Button
                  title="Go Next"
                  //Button Title
                  onPress={() =>
                    navigate('SecondPage', {
                      JSON_ListView_Clicked_Item: this.state.username,
                    })
                  }/>
    
    
    Second:
    
    First Use this line
    
     const { navigate } = this.props.navigation;
    
    then after  {this.props.navigation.state.params.JSON_ListView_Clicked_Item
                ? this.props.navigation.state.params.JSON_ListView_Clicked_Item
                : 'No Value Passed'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-20
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      相关资源
      最近更新 更多