当输入值是文本字段中的变化时,请立即恢复问题
我的应用程序依赖“城市”值来调用API并接收数据栏,为此,我设置了一张表格,使其在我的城市使用setState值,并在我的主应用程序上使用使用效率值。曼城是通过意见提交的,它将养育。问题在于,一旦更改了输入的文本,setCity函数立即关闭,并且不等待提交功能。有帮助吗?
import * as React from 'react';
import '../cssdirect/App.css'
import Icon from './Icon'
import Wind from './Wind'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faDroplet } from '@fortawesome/free-solid-svg-icons'
import { faArrowRight } from '@fortawesome/free-solid-svg-icons'
import TextField from '@mui/material/TextField';
import InputAdornment from '@mui/material/InputAdornment';
import PinDropIcon from '@mui/icons-material/PinDrop';
import { useState, useEffect } from 'react';
const Daily = ({coord, weather, main, visibility, wind, clouds, sys, timezone, name, dt, setCity, city}) => {
const [time, setTime] = useState('')
function timeConverter(dt){
var a = new Date(dt * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var time = date + ' ' + month + ' ' + year + ' ' + hour;
return time;
}
const handleSubmit = (e) =>{
e.preventDefault()
console.log(city)
}
useEffect(()=>{
setTime(timeConverter(dt))
},[])
return (
<>
<h1 className='title'>Monkey Wit Da Weather</h1>
<div className="textInput"
onSubmit={handleSubmit}>
<TextField id="outlined-basic"
placeholder='city'
name='city'
type="text"
onSubmit={(event)=>setCity(event.target.value)}
value={city}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<PinDropIcon/>
</InputAdornment>
),
}} variant="standard" />
<button className="submit" onClick={handleSubmit} type="submit"><FontAwesomeIcon icon={faArrowRight} /></button>
</div>
</>
)
}
export default Daily
app.js
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('Forest%20Hills')
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()
},[city])
if(loading==true){
return <Loading/>
}
else{
return <Daily {...weatherData} setCity={setCity} city={city}/>
}
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这应该是您编码的行为,因为您使用 getweather 的依赖性异步函数(在
使用
city 作为触发app.js
中检查以下代码)。每当City
状态在重新渲染期间发生变化时,就会发生这种触发。您需要的是触发
getweather
async函数进行提交时,而不是更改City
时。因此,将getweather
async函数传递给儿童组件(daily
)作为道具,并在handleseubmit
中称其为以下内容。此外,还要删除前面提到的
使用效果
。顺便说一句,我认为如果您使用
onChange
事件,而不是onsubmit
内部textfield
来自材料UI的组件,则更好。 (参考)Yes, it should be the behavior the way you coded, because you're using an
useEffect
withcity
as the dependency on it to triggergetWeather
async function (check the below code inApp.js
). This trigger happens whenevercity
state changes during re-rendering.What you need is to trigger the
getWeather
async function when you do the submission, not when you changecity
. Therefore, passgetWeather
async function to child component (Daily
) as a prop and call it inside thehandleSubmit
as follows.And additionally, remove the previously mentioned
useEffect
as well.By the way, I think it's better if you use
onChange
event instead ofonSubmit
insideTextField
component from material UI. (reference)