Navigate back to the homepage

how to check empty object in react native?

Infinitbility
React Native
June 28th, 2022 · 1 min read
how to check empty object in react native?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To check the empty object in react native, just use the Object.keys() method it will create an array of object keys after then you can check the length and if it’s equal to 0 means the object is empty.

Like the following example, let’s take the below array of objects example here we will find an index of id 3 and delete it from the array.

1const emptyObject = {};
2
3 // 👇️ Object is empty
4if(Object.entries(emptyObject).length === 0){
5 // 👇️ Object is empty
6 console.log("Object is empty");
7}

Today, I’m going to show you How do I check the empty objects in react native, as above mentioned here, I’m going to use the Object.keys() method with length to check empty objects.

Let’s start today’s tutorial how do you check the empty object in react native?

In this example, we will do

  1. Create a sample empty object state
  2. Check empty objects in a function
  3. Check empty objects in a render method and print on a screen

Let’s write code…

1import React, { useState, useEffect } from 'react';
2import { Text, View, StyleSheet } from 'react-native';
3
4const App = () => {
5 const [obj, setObj] = useState({});
6
7 useEffect(() => {
8 if(Object.keys(obj).length === 0){
9 // 👇️ Object is empty
10 console.log("Object is empty");
11 } else {
12 console.log("Object is not empty");
13 }
14 }, []);
15
16 return (
17 <View style={styles.container}>
18 <Text>{Object.keys(obj).length === 0 ? "Object is empty" : "Object is not empty"}</Text>
19 </View>
20 );
21}
22
23const styles = StyleSheet.create({
24 container: {
25 flex: 1,
26 justifyContent: 'center',
27 backgroundColor: '#ecf0f1',
28 padding: 8,
29 },
30 paragraph: {
31 margin: 24,
32 fontSize: 18,
33 fontWeight: 'bold',
34 textAlign: 'center',
35 },
36});
37
38export default App;

As mentioned above, we are taken the example of a blank object state, use the Object.keys() to convert object keys to the array, and printed it if the object is empty.

Let’s check the output.

React Native, check empty object example
React Native, check empty object example

Try it yourself

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to fix ngrok your account is limited to 1 simultaneous ngrok client session issue?

how to fix ngrok your account is limited to 1 simultaneous ngrok client session issue?

An example of resolve ngrok your account is limited to 1 simultaneous ngrok client session issue.

June 27th, 2022 · 1 min read
how to remove react native cli globally?

how to remove react native cli globally?

An example of remove react native cli globally with step by step guide.

June 27th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]