首页 > 解决方案 > 如何在 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;

标签: javascriptreact-native

解决方案


推荐阅读