[英]How to pass API data to child components
I am trying to create a very simple website called Currency Tracker.我正在尝试创建一个名为 Currency Tracker 的非常简单的网站。 I am using the following API: http://api.nbp.pl/ .
我正在使用以下 API: http : //api.nbp.pl/ 。 I have managed to get the 'mid' and 'code' data with fetch method which I need to render the child components with.
我已经设法使用我需要用来呈现子组件的 fetch 方法获取“mid”和“code”数据。 I'm still learning to use API.
我仍在学习使用 API。 Unfortunatelly I'm stuck.
不幸的是,我被卡住了。 I don't know how to use this data and how pass it down to render the components.
我不知道如何使用这些数据以及如何传递它来呈现组件。
import MainPageItem from './MainPageItem.js'
import './MainPage.css';
fetch('http://api.nbp.pl/api/exchangerates/tables/A')
.then(response => {
return response.json();
})
.then(data => {
const mid = data[0].rates.map(currency => {
return currency.mid
});
console.log(mid)
})
fetch('http://api.nbp.pl/api/exchangerates/tables/A')
.then(response => {
return response.json();
})
.then(data => {
const code = data[0].rates.map(currency => {
return currency.code
});
console.log(code)
})
function MainPage(props) {
return (
<div className="main-page">
<div className="main-page__currecy-list-container">
<div className="main-page__currency-list">
<MainPageItem currency="EUR" mid="4.5" />
<MainPageItem currency="USD" mid="4" />
<MainPageItem currency="GBP" mid="5" />
<MainPageItem currency="CHF" mid="3.5" />
</div>
</div>
</div>
);
}```
The MainPageItem is just a mockup. How can I pas the 'mid' and 'code' data to props render components?. Ideally I want to
You have to put the api calls inside your MainPage
component, store the results inside some state (eg using useState
) and then passing the data wherever you need, or just using it inside the component itself.您必须将 api 调用放在
MainPage
组件中,将结果存储在某个状态中(例如使用useState
),然后将数据传递到您需要的任何地方,或者仅在组件内部使用它。
import MainPageItem from './MainPageItem.js'
import { useState, useEffect } from 'react'
import './MainPage.css';
function MainPage(props) {
const [codes, setCodes] = useState([])
const [mids, setMids] = useState([])
useEffect(() => {
fetch('http://api.nbp.pl/api/exchangerates/tables/A')
.then(response => {
return response.json();
})
.then(data => {
const code = data[0].rates.map(currency => {
return currency.code
});
setCodes(code)
})
fetch('http://api.nbp.pl/api/exchangerates/tables/A')
.then(response => {
return response.json();
})
.then(data => {
const mid = data[0].rates.map(currency => {
return currency.mid
});
setMids(mid)
})
}, [])
return (
<div className="main-page">
<div className="main-page__currecy-list-container">
<div className="main-page__currency-list">
{codes.map((code, i) => <MainPageItem key={i} currency={code} mid={mids[i]} />)}
</div>
</div>
</div>
);
}
This is an example of what you can do in order to save the codes
inside a piece of state of your component.这是一个示例,说明您可以执行哪些操作来将
codes
保存在组件的某个状态中。
The mids
part is very naive and I added it only to give you a hint on how to do that. mids
部分非常幼稚,我添加它只是为了给你一个关于如何做到这一点的提示。
使用道具可以将数据传递给其他组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.