Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

To know the length of an array in react native has the same syntax as javascript.

we have to just use arrayVeriable.length syntax and it will return array element count.

Today, we will learn how to create an array and how to check array length in react native.

Let’s start with the creation

Now, we are going to create an array of objects then we have to store it to state and then we will print their length on screen.

Here is a sample array of objects which we will use in the example.

let users = [
  { id: 1, name: "infinitbility" },
  { id: 2, name: "notebility" },
  { id: 2, name: "repairbility" },
];

To check the length of the array we have the .length option to know the length of the array.

To check the length of exampleArr we have to write exampleArr.length and it will return 3.

import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet } from 'react-native';

export default function App() {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    let users = [
      { id: 1, name: 'infinitbility' },
      { id: 2, name: 'notebility' },
      { id: 2, name: 'repairbility' },
    ];
    setUsers(users);
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>
        users.length is {users.length}
      </Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

Output

react native, array, length, example
React Native, get length of array example.

Try it yourself

Thanks for reading…