我知道有类似的问题,但我不知道为什么会发生错误。Div 显示,但随后应用程序崩溃(好像是一些长度问题)
代码类似于我找到的示例,比如这个沙箱
我究竟做错了什么?
这是组件:
import React, { useState, useEffect } from 'react'
/* import Button from '../Button' */
import { getPlanets } from '../../services/index'
import './Planetas.css'
const Planetas = () => {
const [planetas, setPlanetas] = useState([]);
useEffect(() => {
const fetchPlanetas = async () => {
const planetas = await getPlanets()
setPlanetas({ planetas })
};
fetchPlanetas()
}, []);
return (
<div className="planetas">
{
planetas.map((planeta, key) => {
return <div key={key}>{planeta.name}</div>
})
}
</div>
)
}
export default Planetas
这是 api 服务:
import axios from 'axios'
const BASE_URL = 'https://swapi.co/api/planets'
export const getPlanets = async() => {
const planets = await axios.get(`${BASE_URL}`).catch((e) => {
console.error(e);
})
console.log('resp\n')
console.log(planets.data.results)
return planets.data.results
}
错误: