0

I have an array with several objects. Inside each object i have a field called type that can be either a or b. I am trying to conditional render a specific view for type a and a specific view for type b.

Here is my array:

DATA=[
{id:1, type:'a'},{id:2,type:'b'}
]

return(
{DATA.type === 'a' ?

<Text>I am type A</Text>
:
<Text>I am Type B </Text>
}
)

When i do that nothing appears on the screen.

UPDATE

DATA=[
{id:1, type:'a',locked:true},
{id:2,type:'b',locked:false}
]

const[isLocked,setIsLocked]=useState(false)

return(

{isLocked && DATA.map((item) => item.type === 'a')) ?

<Text>I am type A</Text>
:
null}
)

it is still not working. I am type A still appears on all pages of my carousel.

3
  • DATA is an array. You will need to use index like DATA[0].type === 'a' ? ... Commented Mar 15, 2022 at 11:35
  • @Karan i dont want to specify the object because i am using a carousel Commented Mar 15, 2022 at 12:16
  • @Karan hi can you please look at my updated question Commented Mar 16, 2022 at 6:52

1 Answer 1

1

You need to map your array to accomplish it. Otherwise, you can't get the correct key.

DATA = [
  { id:1, type:a },
  { id:2, type:b },
]

return(
  DATA.map((item) => item.type === 'a' 
    ? <Text>I am type A</Text>
    : <Text>I am Type B </Text>
))
Sign up to request clarification or add additional context in comments.

3 Comments

It is still not working.
Be sure that your syntax for array creation (for RN) is: const DATA = [ { id:1, type:"a" }, { id:2, type:"b" }, ]
this is an typo mistake in my question. Still not workinh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.