I have created a custom function component which has eventlistener from NetInfo for internet connection, which returns a bool value and method name. I wanted to call the method in every class component to check the internet connect and use the bool value to check the connection status.
Below is my code.
//function Component
import React, { PureComponent, useEffect, useState } from 'react';
import NetInfo from "@react-native-community/netinfo";
export default () => {
const [isInternetReachable, setIsInternetReachable] = useState(false)
const InternetChecker = () => {
useEffect(() => {
// Subscribe
const unsubscribe = NetInfo.addEventListener((state) => {
setIsInternetReachable(state.isInternetReachable);
console.log("Connection type", state.type);
console.log("Is internet Reachable?", isInternetReachable);
});
return () => {
unsubscribe();
};
},[])
}
return [InternetChecker, isInternetReachable];
};
Class Component trying to access InternetChecker() method
class HomeScreen extends React.Component {
render() {
const { navigate } = this.props.navigation;
const [InternetChecker, isInternetReachable] = InternetHanlder();
// if (!isInternetReachable) {
// <NoNetworkSign />
// }
InternetChecker()
if (isInternetReachable){
console.log("Internet is Reachable");
} else {
console.log("No Internet Connection");
}
return (
<SafeAreaView style={styles.container}>
<View style={styles.container}>
</View>
</SafeAreaView>
)
};
When i try to access in the above way, i'm getting invalid hook call, Hooks can only be called from inside the body of a function component. How can we call it from a Class Component.
Any help appreciated.