【发布时间】:2021-03-11 07:13:20
【问题描述】:
我正在使用这个 url 获取数据:
我确定我的点表示法是正确的,但是当我尝试将我的 api 信息传递给我的子组件时,会引发错误。
我认为它与async/await有关,或者当页面被渲染时,数据还不能被读取。但我不确定
import React, {useState, useEffect} from 'react';
import SmallBox from './SmallBox';
import './App.css';
function App() {
const [smallData, setSmallData] = useState({})
useEffect(() => {
fetch(`https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&page=1&sparkline=false`)
.then(response => response.json())
.then(data => {
setSmallData(data)
})
})
return (
<div className="app">
{/* SmallBox - bitcoin */}
<SmallBox className="bitcoin" title={smallData[0].name}/>
{/* SmallBox - ethereum */}
<SmallBox className="ethereum" title={smallData[1].name}/>
{/* SmallBox - ripple */}
<SmallBox className="ripple" title={smallData[2].name}/>
{/* SmallBox - tether */}
<SmallBox className="tether" title={smallData[3].name}/>
</div>
);
}
export default App;
【问题讨论】:
标签: javascript reactjs api react-hooks jsx