fetch正在用模板字符串返回一个空数组

发布于 2025-02-08 01:57:25 字数 781 浏览 3 评论 0原文

我对城市的Usestate价值,默认的伦敦,无法与我的GetWeather功能中调用的获取值URL合作。当我不小心重新渲染代码时,它有时会运行,但没有其他时间

import './App.css';
import Daily from './components/daily';
import {useState, useEffect} from 'react'

function App() {

const [city, setCity] = useState('London')
const [weatherData, setWeather] = useState([])

const getWeather = async() =>{
  try{
    const resp = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=f8d957da06af592a1390c360ea801908&units=metric`)
    const pResp = await resp.json()
    setWeather(pResp)
  }
  catch{
    console.log('error')
  }
}

useEffect(()=>{
  getWeather()
},[])
  return(
    <>
    <Daily {...weatherData}></Daily>
    </>
  )
}

export default App;
 code here

My useState value of city, default London, isn't working with the fetch value URL being called in my getWeather function. It runs sometimes when I accidentally re-render the code but not any time else

import './App.css';
import Daily from './components/daily';
import {useState, useEffect} from 'react'

function App() {

const [city, setCity] = useState('London')
const [weatherData, setWeather] = useState([])

const getWeather = async() =>{
  try{
    const resp = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=f8d957da06af592a1390c360ea801908&units=metric`)
    const pResp = await resp.json()
    setWeather(pResp)
  }
  catch{
    console.log('error')
  }
}

useEffect(()=>{
  getWeather()
},[])
  return(
    <>
    <Daily {...weatherData}></Daily>
    </>
  )
}

export default App;
 code here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2025-02-15 01:57:25

问题在于,韦特达塔(Weatherdata
新代码

import './cssdirect/App.css';
import Daily from './components/daily';
import {useState, useEffect} from 'react'
import Loading from './Loading';

function App() {
const [loading, setLoading] = useState(true)
const [city, setCity] = useState('paris')
const [weatherData, setWeather] = useState([])

const getWeather = async() =>{
  setLoading(true)
  try{
    const resp = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=f8d957da06af592a1390c360ea801908&units=imperial`)
    const pResp = await resp.json()
    setWeather(pResp)
    setLoading(false)
  }
  catch{
    console.log('error')
  }
}



useEffect(()=>{
  getWeather()
},[])

  if(loading==true){
    return <Loading/>
    
  }
  else{
    return <Daily {...weatherData}/>
    
  }
}

export default App;

The problem was that the weatherData was being passed through before it fetched and formatted properly, to fix that I just added a loading screen and render delay so the data is always there
New Code

import './cssdirect/App.css';
import Daily from './components/daily';
import {useState, useEffect} from 'react'
import Loading from './Loading';

function App() {
const [loading, setLoading] = useState(true)
const [city, setCity] = useState('paris')
const [weatherData, setWeather] = useState([])

const getWeather = async() =>{
  setLoading(true)
  try{
    const resp = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=f8d957da06af592a1390c360ea801908&units=imperial`)
    const pResp = await resp.json()
    setWeather(pResp)
    setLoading(false)
  }
  catch{
    console.log('error')
  }
}



useEffect(()=>{
  getWeather()
},[])

  if(loading==true){
    return <Loading/>
    
  }
  else{
    return <Daily {...weatherData}/>
    
  }
}

export default App;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文