脚本错误
const initExceptionHandler = () => {
global.ErrorUtils.setGlobalHandler((error) => {
console.log('error', error);
});
};
export default ExceptionHandler ;
使用
import ErrorHandler from './ErrorHandler'
ErrorHandler.initExceptionHandler()
生命周期错误
import React from 'react';
import { Text } from 'react-native';
export default class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <Text>Something went wrong.</Text>;
}
return this.props.children;
}
}
使用
import ErrorBoundaryfrom './ErrorBoundary'
<ErrorBoundary>
<AppNavigation />
</ErrorBoundary>