javascript - 如何在 react Native fetch 中创建自定义警报?
问题描述
我正在尝试使用外部函数自定义错误,但启动模拟器就像AlertView
函数没有触发一样。
import React, { useEffect, useState } from 'react';
import { StyleSheet, ScrollView, Text, ActivityIndicator, View } from 'react-native';
function AlertView() {
return (
<View>
<Text> Error: ....... </Text>
{/* ... */}
</View>
)
}
function SongScreen({ route }) {
const { textArtist, textSong } = route.params;
const [isLoading, setLoading] = useState(true);
const [dataLyrics, setDataLyrics] = useState();
useEffect(async () => {
fetch('https://api.lyrics.ovh/v1/' + textArtist + '/' + textSong)
.then((response) => {
if (response.status == 200)
return response.json()
})
.then((json) => setDataLyrics(json.lyrics))
.catch((error) => AlertView())
.finally(() => setLoading(false));
}, []);
return (
<ScrollView>
{isLoading ? (
<ActivityIndicator size="large" color="black" />
) : (
<Text>
{dataLyrics}
</Text>
)}
</ScrollView>
);
}
export default SongScreen;
解决方案
推荐阅读
- javascript - React 组件没有根据异步调用的响应进行更新
- python - 在多个地方分割字符串Python
- javascript - 如何知道我在 Azure 认知服务语音合成 (TTS) 中使用了多少个字符?
- c# - .net core 3.1 分离模型之间的关系
- node.js - 剧作家拖放
- java - Spring Data JPA 规范 FETCH JOIN 问题
- react-native - 如何使用 Jest 测试 Modal (react-native) 是否可见?
- angular - 订阅和模板离子的问题
- mysql - Maria DB - 为 SQL 事件中的每一行执行存储过程
- python - How do I open an FTP directory so that I can use it the same way I could a normal directory?