Categories
JavaScript Answers React Native Answers

How to check if an array is empty in React Native?

Spread the love

Sometimes, we want to check if an array is empty in React Native.

In this article, we’ll look at how to check if an array is empty in React Native.

How to check if an array is empty in React Native?

To check if an array is empty in React Native, we can use the Array.isArray method and the array length property.

For instance, we write:

import * as React from 'react';
import { View } from 'react-native';

export default function App() {
  const [arr] = React.useState([]);

  if (Array.isArray(arr) && arr.length === 0) {
    console.log('is empty');
  }

  return <View></View>;
}

to check if arr is an empty array with Array.isArray(arr) && arr.length === 0.

Array.isArray checks if its argument is an array.

arr.length === 0 checks if arr is empty.

Conclusion

To check if an array is empty in React Native, we can use the Array.isArray method and the array length property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *