How to Check if Array or Object is Empty in React Js

In this quick example i will show you how to check if object is empty or not in react js. Before mapping the array we have to check or it is good to check that the data array is empty or not in react js.

In this example you will learn that how we can check it whether how to check if json object is empty in react. We can use vanila JavaScript length property check whether the array or object is empty in Reacjs. In this tutorial, i will discuss you empty array is empty or not in reactjs.

Example:

import React from 'react'

export default class Home extends React.Component{
    render(){
        var myArr = [];

        if(myArr.length > 0){
            var items = myArr.map((item) =>
                {item}
            );
        }else{
            var items = "Array is Empty"
        }

        return(
            {items}
        );
    }
}

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using Laravel Vue and React Js Application.