Home > OS >  How to put Text inside map function in React Native?
How to put Text inside map function in React Native?

Time:07-12

I want to put a CusText component when growth Length Img is true. But when I put it in, an Unexpected Token error occurs.

So I put quotation marks or semicolons after the CusText component, but the same error occurs. How do I fix my code?

this is my code

{growthLengthImg[0] ? (

// <CusText />    << i want to put in here
growthLengthImg.map((v) => {
return(
<>
<Pressable>
<Image/>

</Pressable>
</>
)
})

) : (null)}

CodePudding user response:

You did not provide what growthLengthImg[0] data type. But with my guess, if it is empty text '', error will occur. For example,

const [text,setText] = useState("");

return (text ? <Text>{text}</Text> : <OtherComponent />);

To fix it, use Boolean(text) instead of text. Also, check some space, or dot in your component. Even a . or (space), which is not inside <Text> component, can cause error.

CodePudding user response:

you can only return 1 component not 2

  {growthLengthImg[0] ? (
            <>
              <CusText />
              {growthLengthImg.map((v) => {
                return (
                  <>
                    <Pressable>
                      <Image />
                    </Pressable>
                  </>
                );
              })}
            </>
          ) : null}
  • Related