Importing NetInfo from 'react-native' works very well on iOS and android. Although, now trying to import it from @react-native-community/netinfo (version 3.2.1) working on android with no problems but on iOS launching in a repetitive way the addEventListener
. But @react-native-community/netinfo (version 4.. works on iOS without repetitive loop).
Here is my simple code with a console :
import NetInfo from '@react-native-community/netinfo'
onChangeConnexion() {
NetInfo.isConnected.fetch().then(isConnected => {
console.log("hello world!")
if(isConnected) {
this.setState({ netIsConnected: true, netMessage: "" }, () => {
})
} else {
this.setState({ netIsConnected: false, netMessage:
NO_CONNECTION_TRAD[_this.props.lang] })
}
});
}
componentWillUnmount() {
NetInfo.isConnected.removeEventListener('connectionChange',
this.onChangeConnexion)
}
componentDidMount() {
NetInfo.isConnected.addEventListener('connectionChange',
this.onChangeConnexion) // ON CHANGE
}
hello world ! is written in a loop way when launching app. That means that the addEventListener
is called without stop. Is it because i am using the old api of netInfo ?