[英]I am trying to pass the data from an api as props in my react components please can anyone explain what i can do to solve the problem?
When the data is fetched from the API it returns an empty object.当从 API 获取数据时,它返回一个空的 object。 Here is the code below.
这是下面的代码。 It returns an empty object when I console.log then later brings back the object with data in it.
当我使用 console.log 时,它返回一个空的 object,然后稍后带回包含数据的 object。
function App() {
const [wdata, setWdata] = useState({})
const API = "62f84860b69bddcd19d34120487d7375"
useEffect(() => {
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=${API}`)
.then(response => response.json())
.then(resp => setWdata(resp))
.catch(error => console.log(error))
}, [Left])
console.log(wdata)
return (
<div className="App">
<Left className="left" name={wdata.sys.country} temp={wdata.main.temp} description={wdata.weather[0].description} />
<Right className="right" />
</div>
);
}
You can display a loader at the time of fetching data from the Api.
Because data coming from the API needs some time.
Try尝试
function App() {
const [wdata, setWdata] = useState(null)
const API = "62f84860b69bddcd19d34120487d7375"
const fetchData = async() => {
await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=${API}`)
.then(response => response.json())
.then(resp => setWdata(resp))
.catch(error => console.log(error))
}
useEffect(() => {
fetchData()
}, [Left])
console.log(wdata)
return (
<div className="App">
{wdata ? <Left className="left" name={wdata.sys.country} temp={wdata.main.temp} description={wdata.weather[0].description} /> : <Left className="left" name={"loading..."} temp={"loading..."} description={"loading..."} /> }
<Right className="right" />
</div>
);
}
Apparently using async, await waits for the API to return the response and stores in your state.显然使用异步,await 等待 API 返回响应并存储在您的 state 中。 Before the response is received, your data will be displaying loading...
在收到响应之前,您的数据将显示正在加载...
As I said in my comment, the console.log()
will always be executed before the .then()
calls.正如我在评论中所说,
console.log()
将始终在.then()
调用之前执行。 This is how the JS event loop works.这就是 JS 事件循环的工作方式。 If you want to execute code after
setWdata(resp)
then simply put that code inside the .then()
call.如果你想在
setWdata(resp)
之后执行代码,那么只需将该代码放在 .then .then()
调用中。
useEffect(() => {
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=${API}`)
.then(response => response.json())
.then(resp => {
setWdata(resp);
doStuffAfter(); // Executes after setWdata
})
.catch(error => console.log(error))
}, [Left]);
doStuffBefore(); // Executes before all then() calls
Can you please try this once?你能试试这个吗?
function App() {
const [wdata, setWdata] = useState({})
const [isLoading, setIsLoading] = useState(false);
const API = "62f84860b69bddcd19d34120487d7375"
useEffect(() => {
setIsLoading(true);
fetch(`https://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=${API}`)
.then(response => response.json())
.then(resp => setWdata(resp))
.catch(error => console.log(error))
setIsLoading(false);
}, [])
console.log(wdata)
if(isLoading){
return <loader/>;
}
return (
<div className="App">
<Left className="left" name={wdata.sys.country} temp={wdata.main.temp} description={wdata.weather[0].description} />
<Right className="right" />
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.