簡體   English   中英

TypeError: this.props.navigation.getParam is not a function 同時使用 ReactNative 傳遞參數

[英]TypeError: this.props.navigation.getParam is not a function while passing parameter using ReactNative

通過 getParam 方法訪問參數時出現錯誤,如下所示。

    const source = this.props.navigation.getParam("source","0")
    const doFollow = this.props.navigation.getParam("doFollow","")

我已經使用以下方法傳遞了參數

this.props.navigation.navigate('Details', {otherParam:"anything"})

這取決於您使用的react-navigation版本。

在 v4 中

this.props.navigation.getParam('source')

在 v5 中

this.props.route.params.source

它可能不完全適合您的示例,但我正在傳遞/檢索這樣的數據:

this.props.navigation.navigate('DisplayPage', { meetId: item.ID })

然后在接收頁面

props.navigation.state.params.meetId

如果您使用的是最新版本的反應導航(即版本 5.0 或更高版本)。 您可以簡單地使用this.props.route.params.YOUR_PARAMETER_KEY來傳遞參數

如果您想將數據(例如 JSON)從一個 class 發送到 React-Native 中的另一個。

this.props.navigation.navigate('Detail Screen', json)

這里 json 是帶有鍵值對的 JSON object。 前任。

    { id: 1234, name: "Dean", age: 30}

在接收器類中/在渲染中導航 Class

     const name = this.props.route.params.name;
     const age = this.props.route.params.age;

注意:請確保數據作為鍵值 {key: value} 發送

假設您使用的是最新的react-navigation 你犯了一個小錯誤。

嘗試從 function 中刪除第二個參數。

let source = this.props.navigation.getParam("source")
let doFollow = this.props.navigation.getParam("doFollow")

if(!source) source = "0"
if(!doFollow) doFollow = ""

props.navigation.getParams(**source**) is not working in V4

props.route.params.**source** in at V5

V5:

沒有 object 解構 -->

function(props)
{
   props.navigation.goBack() //For navigation
   props.route.params.**source** //For params
} 

使用 object 解構 -->

function({navigation, route})
{
   navigation.goBack()    //FOR navigation
   route.params.**source**   //FOR params
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM