Shijin TR
Shijin TR

Reputation: 7768

Array loop inside conditional statement react-native not working

I was trying to implement array iteration inside a conditional statement in react native like this,

{!loading&&documents.length>0?(
   {documents.map((item, i) => {
    return (
    <ViewButton>mybutton</ViewButton>
    )}}

   ):(
   null
 )}

But it shows syntax error in console

Unexpected token, expected "," (43:21)

41 | 42 |
{!loading&&documents.length>0?(

43 | {documents.map((item, i) => { | ^ 44 | return ( 45 | sss 46 | )}}

Upvotes: 0

Views: 37

Answers (2)

Rishav Kumar
Rishav Kumar

Reputation: 5460

Try this

  {!loading&&documents.length>0?(
   documents.map((item, i) => {
    return (
    <ViewButton>mybutton</ViewButton>
    )}

   )):(
   null
 )}
  }

Upvotes: 0

Blimeys
Blimeys

Reputation: 176

You have a syntax issue, this should work:

  !loading && documents.length > 0
? documents.map((item, i) => {
    return <ViewButton>mybutton</ViewButton>;
  })
: null;

I'd highly recommend to use a linter (eslint) and something like prettier so you don't run in issues like these.

Upvotes: 2

Related Questions