這是我的代碼Iam使用 API 并從中獲取資料...如果iam控制臺表示它可以正常作業,但does'nt在使用時會顯示它FlatList
Text,FlatList,Button,TouchableOpacity從'react-native'匯入{視圖, };從“反應”匯入反應,{ useState,useEffect};
const LyricsGet = () => {
const url = "https://api.lyrics.ovh/v1/Sia/unstoppable";
const[lyric,setLyric]=useState([]);
Fetching the data using useEffect
useEffect(() => {
console.log("inside the useeffect");
fetch(url)
.then(response => response.json())
.then(results => {
console.log(results);
setLyric(results);
})
.catch(err => {
setError(err);
});
console.log("inside the useeffect 22222");
}, []);
return (
<View>
<FlatList
data={lyric}
renderItem={({ item }) => <Text key={item._id}>{item.lyrics}</Text>}
keyExtractor={item => item.id}
/>
</View>
);
}
這里我的控制臺
Object {“歌詞”:“我會微笑,我知道如何愚弄這個小鎮想聽我流淚時戴上墨鏡 這從來都不是正確的時間
我穿上盔甲,讓你看看我有多堅強
我穿上我的盔甲,我會告訴你我是
我勢不可擋
我是一輛沒有剎車的保時捷
我是無敵的
Yeah, I win every single game
I'm so powerful
I don't need batteries to play
I'm so confident
Yeah, I'm unstoppable today
Unstoppable today, unstoppable today
Unstoppable today, I'm unstoppable today
Break down, only alone I will cry out now
You'll never see what's hiding out
Hiding out deep down, yeah, yeah
I know, I've heard that to let your feelings show
Is the only way to make friendships grow
But I'm too afraid now, yeah, yeah
I put my armor on, show you how strong I am
I put my armor on, I'll show you that I am
I'm unstoppable
I'm a Porsche with no brakes
I'm invincible
Yeah, I win every single game
I'm so powerful
I don't need batteries to play
I'm so confident
Yeah, I'm unstoppable today
Unstoppable today, unstoppable today
Unstoppable today, I'm unstoppable today
Unstoppable today, unstoppable today
Unstoppable today, I'm unstoppable today
I put my armor on, show you how strong I am
I put my armor on, I'll show you that I am
I'm unstoppable
I'm a Porsche with no brakes
I'm invincible
Yeah, I win every single game
I'm so powerful
I don't need batteries to play
I'm so confident
Yeah, I'm unstoppable today
Unstoppable today, unstoppable today
Unstoppable today, I'm unstoppable today
Unstoppable today, unstoppable today
Unstoppable today, I'm unstoppable today", }
uj5u.com熱心網友回復:
您的結果變數是一個物件。
您可以通過在設定歌詞時創建一個陣列來實作您想要的:
setLyric([results]);
uj5u.com熱心網友回復:
您從 useState lyric.. 獲得的資料是一個物件,對嗎?FlatList 需要一個陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/446873.html
