I'm currently working on an application that scans 2D barcodes to retrieve data from the URLs provided by these codes. When a user loses internet connection, the application stores the URLs using AsyncStorage. However, I am facing an issue where I need to implement a listener that triggers a specific method once the internet connection is regained. Can someone suggest the best approach for implementing such a connection listener?
Edit:
I attempted to use a NetInfo EventListener, but it seems like I might be using it incorrectly because the function gets called every time, even when there's no change in internet status.
_connectionHandler = (e) => {
this.setState({ cameraActive: false })
NetInfo.getConnectionInfo().then((connectionInfo) => {
if (connectionInfo.type === "none"){
console.log("No internet")
dataArray.push(e.data)
let barcodeData_delta = {
data: dataArray
}
AsyncStorage.mergeItem(STORAGE_KEY, JSON.stringify(barcodeData_delta));
NetInfo.isConnected.addEventListener(
'connectionChange',
this._handleConnectionChange(e.data)
);
this.setState({ cameraActive: true })
} else {
console.log("Internet available -> Going to read barcode now")
this._handleBarCodeRead(e.data);
}
})
}