Hi Friends 👋,
Welcome To Infinitbility! ❤️
To check null value in react native, use if
statement it will handle all falsy value or you can compare with null
it self.
Let’s see short example to use if
statement to handle null value.
let value = null;
if(value){
console.log("value have data")
} else {
console.log("value is null or falsy value")
}
Today, I’m going to show you How do I check null value in react native, as above mentioned, I’m going to use the above-mentioned use if
statement.
Let’s start today’s tutorial how do you check null value in react native?
React native check null value example using if
Here, we will take example of string, number, boolean, object and undefined datatype.
import React, { useState, useEffect } from "react";
import { Text, View } from 'react-native';
export default function App() {
const [value, setValue] = useState(null);
useEffect(() => {
if(value){
console.log("value have data")
} else {
console.log("value is null or falsy value")
}
}, []);
return (
<View>
<Text>{value ? "value have data" : "value is null or falsy value"}</Text>
</View>
);
}
Output
React native check null value example compare with null
Here, we will compare value with null and also handle if value have null as string.
import React, { useState, useEffect } from "react";
import { Text, View } from 'react-native';
export default function App() {
const [value, setValue] = useState(null);
useEffect(() => {
if(value == null || value == "null"){
console.log("value is null")
} else {
console.log("value have data")
}
}, []);
return (
<View>
<Text>{(value == null || value == "null") ? "value is null" : "value have data"}</Text>
</View>
);
}
Output
I hope it helps you, All the best 👍.