2 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
你快到了。您的 axios 調(diào)用需要稍微改變一下。以下是修復(fù)方法:
import React from "react";
import axios from "axios";
import "./styles.css";
export default function App() {
const [results, setResults] = React.useState([]);
const profileData = async () => {
try {
const res = await axios
.get("https://randomuser.me/api/?results=10")
.then((result) => result.data.results);
setResults(res)
} catch (err) {
console.log(err);
}
};
React.useEffect(() => {
profileData();
}, []);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
{results.map((person) => {
console.log("PERSON", person);
})}
</div>
);
}
注意這部分:
const res = await axios
.get("https://randomuser.me/api/?results=10")
.then((result) => result.data.results);
另請(qǐng)記住,console.log("PERSON", person);在返回函數(shù)內(nèi)部實(shí)際上不會(huì)在頁面上呈現(xiàn)任何內(nèi)容,它將打印到控制臺(tái)。只是想澄清這一點(diǎn)。
沙盒:https://codesandbox.io/s/exciting-shockley-9m5vj? file=/src/App.js
添加回答
舉報(bào)