最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: this.props.navigation.getParam is not a function while passing parameter using ReactNative - Stack Overf

programmeradmin2浏览0评论

I am getting an error while accessing parameter by the getParam method like following .

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

I have passed the parameter using the following method

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

I am getting an error while accessing parameter by the getParam method like following .

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

I have passed the parameter using the following method

this.props.navigation.navigate('Details', {otherParam:"anything"})
Share Improve this question edited Apr 9, 2020 at 18:15 Raghu asked Apr 9, 2020 at 17:15 RaghuRaghu 1251 gold badge2 silver badges7 bronze badges 1
  • 2 Can you add some more details please ? – Dipansh Khandelwal Commented Apr 9, 2020 at 17:31
Add a ment  | 

6 Answers 6

Reset to default 11

It depends on your react-navigation version that you are using.

In v4

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

In v5

this.props.route.params.source

It may not fit your example pletely but i'm passing/retrieving data like this:

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

Then on receiving page

props.navigation.state.params.meetId

If You are using the latest version of react navigation(i.e. Version 5.0 or more). You can simply use this.props.route.params.YOUR_PARAMETER_KEY for passing parameters

If you want to send data(ex. JSON) from one class to another in React-Native.

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

Here json is the JSON object with key-value. Ex.

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

In the receiver class/ Navigated Class in render

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

NOTE:Please make sure data is send as key value {key: value}

Assuming that you are using the latest react-navigation. You are making a small mistake.

Try removing the second parameter from the 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:

without object destructuring -->

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

with object destructuring -->

function({navigation, route})
{
   navigation.goBack()    //FOR navigation
   route.params.**source**   //FOR params
} 
发布评论

评论列表(0)

  1. 暂无评论