Heim > Fragen und Antworten > Hauptteil
P粉2079697872023-08-26 14:12:53
您需要在useEffect Hook中包装异步API调用,并将数据存储在状态中,以便在渲染函数中使用该状态。以下是一个没有测试的示例代码:
function Banners(props) { const [response, setResponse] = useState([]); const fetchData = async () => { const response = await axios.get(`${apiUrl}/assets/get`); setResponse(response); }; useEffect(() => { fetchData(); }, []); return ( <MainContent text={response.text} img1={props.img1 ? props.img1 : response.data.img1} img2={props.img2 ? props.img2 : response.data.img2} /> ); }