onChange 不适用于文本字段中的第一个输入

发布于 2025-01-14 00:14:57 字数 1281 浏览 4 评论 0原文

onChange 事件不会返回文本输入的第一个更改,但适用于后续更改。抱歉,如果这很明显,我是 React 新手。

import React, { useState } from 'react';
import axios from 'axios'; 

function Header() {

    const [text, setText] = useState("");
    const [location, setLocation] = useState("");

    function handleChange(event) {
        setText(event.target.value);
    }

    // when submit button clicked, current state of the text input is stored inside location constant 
    function handleClick() {
        setLocation(text);
        const geoCoderURL = "http://api.openweathermap.org/geo/1.0/direct?q=" + location + "&limit=5&appid={apikey}"
        function getCoordinates(url) {
            axios.get(url).then((response) => {
                const locationLat = response.data[0].lat;
                const locationLon = response.data[0].lon;
                
            });
        } 
        getCoordinates(geoCoderURL);        
    }
    return (
        <div>
            <h1>Five Day Forecast</h1> 
            <input onChange={handleChange} type="text" name="name"autoFocus placeholder="Enter location here."/>
            <button type="submit" onClick={handleClick}>Forecast</button> 
        </div>
    )
}

export default Header;

onChange event doesn't return the first change of the text input but works on subsequent changes. Sorry if this is obvious, I'm new to React.

import React, { useState } from 'react';
import axios from 'axios'; 

function Header() {

    const [text, setText] = useState("");
    const [location, setLocation] = useState("");

    function handleChange(event) {
        setText(event.target.value);
    }

    // when submit button clicked, current state of the text input is stored inside location constant 
    function handleClick() {
        setLocation(text);
        const geoCoderURL = "http://api.openweathermap.org/geo/1.0/direct?q=" + location + "&limit=5&appid={apikey}"
        function getCoordinates(url) {
            axios.get(url).then((response) => {
                const locationLat = response.data[0].lat;
                const locationLon = response.data[0].lon;
                
            });
        } 
        getCoordinates(geoCoderURL);        
    }
    return (
        <div>
            <h1>Five Day Forecast</h1> 
            <input onChange={handleChange} type="text" name="name"autoFocus placeholder="Enter location here."/>
            <button type="submit" onClick={handleClick}>Forecast</button> 
        </div>
    )
}

export default Header;

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

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

发布评论

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

评论(1

ぃ双果 2025-01-21 00:14:58

当您调用 setLocation(text); 时,handleClick 将在 location 值实际更新之前完成执行。

不需要 locationsetLocation;看起来也许 location 应该与 text 相同。

function handleClick() {
    const geoCoderURL = "http://api.openweathermap.org/geo/1.0/direct?q=" + text + "&limit=5&appid={apikey}"
    function getCoordinates(url) {
        axios.get(url).then((response) => {
            const locationLat = response.data[0].lat;
            const locationLon = response.data[0].lon;  
        });
    } 
    getCoordinates(geoCoderURL);        
}

When you call setLocation(text);, handleClick will finish execution before the location value is actually updated.

There is no need for location and setLocation; looks like maybe location is supposed to be the same as text.

function handleClick() {
    const geoCoderURL = "http://api.openweathermap.org/geo/1.0/direct?q=" + text + "&limit=5&appid={apikey}"
    function getCoordinates(url) {
        axios.get(url).then((response) => {
            const locationLat = response.data[0].lat;
            const locationLon = response.data[0].lon;  
        });
    } 
    getCoordinates(geoCoderURL);        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文