我试图在我的应用程序中使用try catch来管理错误。例如,在这种情况下,如果没有连接,在没有代码推送干扰的情况下捕获应用程序的错误。如果出现错误,只需停止codepush,然后继续应用程序的功能。
下面是我的代码:import React, {useEffect, useState} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import codePush from 'react-native-code-push';
import {LoaderBox, ProgressBox} from '@components';
import {Navigator} from '@navigator';
export default function App() {
const [syncMessage, setSyncMessage] = useState(true);
const [syncProgress, setSyncProgress] = useState(false);
const [progress, setProgress] = useState(0);
const codePushStatusDidChange = (status: any) => {
try {
switch (status) {
case codePush.SyncStatus.CHECKING_FOR_UPDATE:
setSyncMessage(true);
break;
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
console.log('Downloading package.');
setSyncMessage(false);
setSyncProgress(true);
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
console.log('Installing update.');
setSyncProgress(true);
setSyncMessage(false);
break;
case codePush.SyncStatus.UP_TO_DATE:
console.log('Up-to-date.');
setSyncMessage(false);
break;
case codePush.SyncStatus.UPDATE_INSTALLED:
console.log('Update installed.');
setSyncProgress(false);
break;
}
} catch (err) {}
};
const codePushDownloadDidProgress = (progression: {
receivedBytes: any;
totalBytes: any;
}) => {
let progressReceived = Math.round(
(progression.receivedBytes / progression.totalBytes) * 100,
);
setProgress(progressReceived);
};
useEffect(() => {
codePush.sync(
{installMode: codePush.InstallMode.IMMEDIATE},
codePushStatusDidChange,
codePushDownloadDidProgress,
);
}, []);
return (
<NavigationContainer>
{syncMessage && <LoaderBox />}
{!syncMessage && !syncProgress && <Navigator />}
{syncProgress && <ProgressBox prog={progress} />}
</NavigationContainer>
);
}
提前感谢您的帮助
像这样包装函数:
const codePushStatusDidChange = (status: any) => {
try {
switch (status) {
case codePush.SyncStatus.CHECKING_FOR_UPDATE: {
setSyncMessage(true);
break;
}
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
console.log('Downloading package.');
setSyncMessage(false);
setSyncProgress(true);
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
console.log('Installing update.');
setSyncProgress(true);
setSyncMessage(false);
break;
case codePush.SyncStatus.UP_TO_DATE:
console.log('Up-to-date.');
setSyncMessage(false);
break;
case codePush.SyncStatus.UPDATE_INSTALLED:
console.log('Update installed.');
setSyncProgress(false);
break;
}
} catch (err) {}
setSyncMessage(false);
};