Javascript 尝试将unix时间戳转换为天,但打印的天数相同。使用react

Javascript 尝试将unix时间戳转换为天,但打印的天数相同。使用react,javascript,reactjs,api,Javascript,Reactjs,Api,我正在调用一个api,它提供7天的每日预测。我写了一些代码试图将dt unix转换为天..但我得到的是相同的时间,而不是多次。。。https://gyazo.com/4f9d207ea2c3350c5fc30fa66d344634 Api json- 问题->https://gyazo.com/009e8367331c34ceb205e42d0710e142 我的代码- import Card from './components/Card'; import axios from 'axios'

我正在调用一个api,它提供7天的每日预测。我写了一些代码试图将dt unix转换为天..但我得到的是相同的时间,而不是多次。。。https://gyazo.com/4f9d207ea2c3350c5fc30fa66d344634

Api json-

问题->https://gyazo.com/009e8367331c34ceb205e42d0710e142

我的代码-

import Card from './components/Card';
import axios from 'axios';
import './App.css';



function App() {
const [query, updateQuery] = useState()
const [weather, updateWeather] = useState()
const [forecast, updateForecast] = useState()


const search = (evt) => {
  if (evt.key === "Enter"){
    axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${query}&appid=`).then((res) => {
      const currentData = res.data;
      
      updateQuery("")
      updateWeather(currentData)
      
      axios.get(`https://api.openweathermap.org/data/2.5/onecall?lat=${currentData.coord.lat}&lon=${currentData.coord.lon}&exclude=current,hourly,minutely,alerts&units=metric&appid=`).then((res) => {
        const forecastData = res.data;
 
  updateForecast(forecastData)


  const days = ['Friday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday'];

const fiveForecastDays = forecast.daily.map(({ dt }) => days[new Date(dt).getDay()]);
console.log(fiveForecastDays);
  
  
})

    })
   
  }
}




const queryHandler=(e)=>{
  // console.log(e.target.value)
  updateQuery(e.target.value)
  }
  

  return (
    <div className="App">
      <input onChange={queryHandler} value={query} onKeyPress={search} type="text"/>
        
    
    </div>

  );
}

export default App;
从“./components/Card”导入卡;
从“axios”导入axios;
导入“/App.css”;
函数App(){
const[query,updateQuery]=useState()
const[weather,updatewather]=useState()
常量[forecast,updateForecast]=useState()
常量搜索=(evt)=>{
如果(evt.key==“输入”){
axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${query}&appid=`。然后((res)=>{
const currentData=res.data;
updateQuery(“”)
UpdateWather(当前数据)
axios.get(`https://api.openweathermap.org/data/2.5/onecall?lat=${currentData.coord.lat}&lon=${currentData.coord.lon}&exclude=current、hourly、minutely、alerts&units=metric&appid=`。然后((res)=>{
const forecastData=res.data;
更新预测(预报数据)
常量天数=[“星期五”、“星期日”、“星期一”、“星期二”、“星期三”、“星期四”];
const fiveForecastDays=forecast.daily.map({dt})=>days[newdate(dt.getDay());
控制台日志(五个预报日);
})
})
}
}
常量queryHandler=(e)=>{
//console.log(例如target.value)
updateQuery(例如target.value)
}
返回(
);
}
导出默认应用程序;

由于状态更新的异步性质,在执行以下操作时:

updateQuery("")
updateWeather(currentData)
它不会立即更新,因此下一行中的请求将与前一个状态一起发送

要解决此问题,您应该使用
useffect
,从该状态更新时触发的副作用触发请求:

useEffect(() => {
    axios.get(`https://api.openweathermap.org/data/2.5/onecall?lat=${currentData.coord.lat}&lon=${currentData.coord.lon}&exclude=current,hourly,minutely,alerts&units=metric&appid=`).then((res) => {
    const forecastData = res.data;
    updateForecast(forecastData)
    // ....
   
}, [currentData]);

您的第一个请求也可能是
updateQuery
更改的副作用。

@braindead401不,如果您没有收到,请继续(在手机上键入无法再格式化内容)-repost#1您能在函数中添加useEffect吗(以我的示例为例,搜索函数?#2你认为我的第一个api调用也应该使用useEffect吗?#3我在尝试映射响应时也没有定义..关于解决方案的任何想法1.useEffect应该在函数组件的顶层(不能在嵌套函数中)2.是的。3.如果(!mystate){return},你可能需要
if(!mystate){return
保护其内部不向api发送无效值我不确定如何处理我的示例,因为它们存在很多范围问题,并且我的搜索功能(如果按输入键,则api被提取)在useEffect中正常工作。我是否能够放下我的代码,然后可能让您稍后再查看?已解决,谢谢!!!