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> |
Output of above example-
Advertisements
Add Comment
📖 Read More
- 1. ReactJS Reload Page
- 2. ReactJS check element exists in Array
- 3. React Pass Component As Prop with Props | ReactJs
- 4. ReactJs Round to two decimal places