ReactJs check empty Array or Object


We can use native JavaScript length property check whether the array or object is empty in Reacjs. Here in this tutorial, we are going to explain how you can check null array or object in Reactjs. You can also use our online editor to edit and run the code online.


ReactJs check empty Array or Object Example

You can check empty array or object in Reactjs simply as below-

Example:

<div id="root">
</div>
<script type="text/es2015">
function MyFunction() {
 var myArray = [];
 if(myArray.length > 0){
 var items = myArray.map((item) =>
  <li>{item}</li>
);
}else{
 var items = <li>Array is Empty.</li>
}
  return (
    <ul>{items}</ul>
  );
}

ReactDOM.render(
  <MyFunction />,
  document.getElementById('root')
);
</script>

Try it »

Output of above example-

ReactJs check empty Array or Object

Advertisements

Add Comment

📖 Read More