Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Variable inside jsx loop

I have loop like this inside jsx render

return(
<View>
   {[...Array(10)].map((x, i) => (
          <Input
            placeholder="Category {i}"
          />
        ))}
</View>
)

I am trying to create 10 inputs dynamically using above code , here I want to use the variable I inside place holder so I get the inputs with placeholder Category 1 , Category 2 etc., but my app is crashing without any error , what is the proper way to use variable inside the map of jsx.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe try this

return (
  <View>
    {new Array(10).map((x, i) => (
      <Input placeholder={`Category ${i + 1}`} />
    ))}
  </View>
);

Refer to this

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading