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

How to get first element in array in React Native

I tried to get only first element [0] of array and print in UI.
I will get only first lesson in the array and then print.

{route.params.Lessons.map((lessons) => {
              return (
                <View>
                  <Text style={styles.lessonTitle}>Lesson 1</Text>
                  <Text style={styles.lessonDescription}>{lessons.lessonName}</Text>
                </View>
              )
            })}

enter image description here

Thanks

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 :

If you only want to display only one element of array, you don’t need to use the .map() function

You can try this instead

const lesson1 = route.params.Lessons[0]

return (
    <View>
        <Text style={styles.lessonTitle}>Lesson 1</Text>
        <Text style={styles.lessonDescription}>{lesson1.lessonName}</Text>
    </View>
)

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