Warning: session_start(): open(/tmp/sess_713b5dcd29f63b5d689e329890d8d313, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If An Array Is Empty In React - LearnShareIT

How To Check If An Array Is Empty In React

How To Check If An Array Is Empty In React

To check if an array is empty in React, you can compare the length of the array with 0. Learn more about it with the explanation and examples below.

Array and its properties

An array is a data type whose value contains many other values. Each value of the array is called an element.

The length property

An attribute that gives the length of the array. If you use a negative integer, an actual number or a string as the index, the array’s length is also the highest positive integer.

Some ways to check if an array is empty in React

Compare if Array.length equals to 0

In this way, we will use the length property of array mentioned above to get the length of the array. Using the method below, we will check the cars array. Let’s see an example that can be applied to real problems below to understand how this method works.

Code:

import { useEffect, useState } from 'react';
 
export default function App() {
    const [cars, setCars] = useState([]);
    const [currentCar, setCurrentCar] = useState("");
    useEffect(() => {
        if (cars.length === 0) {
          	console.log('Array is empty');
        } else {
          	console.log('Array is not empty');
        }
    }, [cars]);

    return (
        <div>
            <h1>Cars: {JSON.stringify(cars)}</h1>
            <input 
              	type="text" 
              	name="cars" 
              	placeholder="Enter car name" 
              	onChange={(e) => { setCurrentCar(e.target.value); }}
             />
            <button onClick={() => setCars(current => [...current, currentCar])}>
            	Add Car
            </button>
        </div>
    );
}

Output:

Array is empty

In this example, firstly, we have an empty cars array. When comparing its length with the 0 value, it will return true and caught in the if statement, so the program printed the console: ‘Array is empty’. But after we added the string car array to the array, the array was no longer empty and returned a different value.

Compare if Array.length is greater than 0

In this way, we also use the Array.length method to get the length value of the array and compare it. However, the logic will be slightly different in that we will find if it is greater than 0. If more significant, the array is not empty and reverse, so we also get the same result as above.

Code:

import {useEffect, useState} from 'react';

export default function App() {
    const [cars, setCars] = useState([]);
    const [currentCar, setCurrentCar] = useState("");
    useEffect(() => {
        if (cars.length > 0) {
          	console.log('Array is not empty');
        } else {
          	console.log('Array is empty');
        }
    }, [cars]);

    return (
        <div>
            <h1>Cars: {JSON.stringify(cars)}</h1>
            <input 
              	type="text" 
              	name="cars" 
              	placeholder="Enter car name" 
              	onChange={(e) => { setCurrentCar(e.target.value); }}
            />
            <button onClick={() => setCars(current => [...current, currentCar])}>
            	Add Car
            </button>
        </div>
    );
}

Output:

Array is empty

We’ve gone through two examples of two simple ways to check if an array is empty or not. According to my research, we can also find the length value of the array using the Array.size() method. Although the program still runs, it is not a smart way. I hope this article helps you.

Summary

To summarize, this article shows you two ways to check if an array is empty in React. You can compare if Array.length equals to 0 methods or if Array. length is greater than 0 to get the same result. Let’s try them!

Maybe you are interested:

Leave a Reply

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