我在我的代碼中收到此錯誤:“TypeError:無法讀取未定義的屬性(讀取‘地圖’)”,我完全不知道為什么,我的代碼 IDE 本身似乎沒有錯誤,而且我有查了很多遍了謝謝閱讀!
const { user } = useAuth0();
const [{songs, setSongs}] = useState([])
useEffect(() => {
if (user) {
const requestOptions = {
method: 'GET'
};
let url = 'https://present-deck.com/getsongs/' user.sub
fetch(url, requestOptions)
.then(response => {
return response.json();
}).then(jsonResponse => {
setSongs(jsonResponse)
}).catch (error => {
console.log(error);
})
console.log(songs)
}
}, [user])
return (
<>
<ul>
{songs.map((el) => (
<li key={el} className="tailwindCssStuff"
onClick={ () => {if (listedSongs.indexOf(el.title) === -1){
addListedSongs(listedSongs.concat(el.title))}}}>
{el.title}</li>
))}
</ul>
</>
)
}```
uj5u.com熱心網友回復:
const [{songs, setSongs}] = useState([])
應該 const [songs, setSongs] = useState([])
uj5u.com熱心網友回復:
在解構狀態時,您有一組不必要的大括號,這會導致您的songs和setSongs未定義。
相反,您想要:
const [songs, setSongs] = useState([])
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370890.html
上一篇:當我嘗試為串列中的每個專案創建一個組件時,Reactjs會覆寫組件
下一篇:ReactAntDesignform.resetFields()不呼叫<Form.Items>的onChange事件
