j.doe
j.doe

Reputation: 4859

How to properly catch an error with react native

I am new with react native and I am trying catching an error in this way

Linking.openURL('test').catch(err =>
     alert('error' + err)
   );

But If I try to log the error like this

Linking.openURL('test').catch(err =>
     alert('error' + err); // error in this line
     console.log(err);
   );

I get a syntax error because of the comma.

Upvotes: 0

Views: 1695

Answers (1)

alayor
alayor

Reputation: 5025

You're just missing the braces in the arrow function.

Linking.openURL('test').catch(err => {
   alert('error' + err); // error in this line
   console.log(err);
});

You need braces if your arrow function has more than one sentence.

If you don't have braces, you should only have one sentence whose result will be automatically returned.

Upvotes: 2

Related Questions