Tianyun Ling
Tianyun Ling

Reputation: 1097

Get passing variable by name error

I am new to react native, and I can successfully run navigate example (https://reactnavigation.org/docs/en/params.html) locally:

But I don't understand, why this code:

const { navigation } = this.props;
const itemId = navigation.getParam('name', 'NO-ID');

can succefully get the value of variable 'name', but if I modified it to:

//const { navigation } = this.props;
const itemId = this.props.getParam('name', 'NO-ID');

Android emulator would complain:

undefined is not a function (evaluating  const itemId = this.props.getParam('name', 'NO-ID') )

?

Since { navigation } would be same as this.props ?

Upvotes: 0

Views: 1632

Answers (2)

Hariharan L
Hariharan L

Reputation: 1421

you have confused with es6 destructuring.

const { navigation } = this.props;

is equal to

const navigation = this.props.navigation;

It is just a syntactic sugar. In your case you should correct your code like below:

const itemId = this.props.navigation.getParam('name', 'NO-ID')

Upvotes: 3

You forgot this.props.NAVIGATION :)

// const { navigation } = this.props;
const itemId = this.props.navigation.getParam('name', 'NO-ID');

Upvotes: 2

Related Questions