我是个新手,对Reaction和Java脚本很陌生,正在try 访问一个公共API.我有以下功能:

import { useState } from "react";
import CountrySWR from "./CountrySWR";


export default function SearchForm(){
    const [countryName, setCountryName] = useState("a");

    function handleSubmit(e) {
        e.preventDefault();

        const query = e.target("query");
        setCountryName(query);
    }

    return (
        <>
            <form onSubmit={handleSubmit}>
                <input name="query"/>
                <button type="submit">Search</button>
            </form>
            <CountrySWR name={countryName}/>
        </>
    );
};

并且CountrySWR.js如下所示:

import useSWR from "swr";
import { useState } from "react";


const fetcher = (...args) => fetch(...args).then((res) => res.json());

export default function CountrySWR({name}){

    const url = "https://restcountries.com/v3.1/name/" + name;

    const {
        data : countries,
        error,
        isLoading,
    } = useSWR(url, fetcher);


    if(error) return <div className="failure">Failed to load data</div>;
    if(isLoading) return <div className="loading">Loading, please wait...</div>

    if(length > 1){
        txt = 'Territories in ' + name;
    }


    return(
        <div style={{ columnns: "150px 6", padding: "10px", margin: '20px', width: 'fit-content'}}>
            <h2>{txt}</h2>
            {countries &&
                countries.map((country, index) => (
                    <div key={index} style={{margin: '20px', border:"1px solid gray", padding: '20px',
                    borderRadius: '10px', display: 'inline-block'}}>
                        <h4>{country.name.common}</h4>
                        <p>Population: {country.population.toLocaleString('en-US')}</p>
                        {country.name.special !==  null ? <p>{country.name.special}</p> : <p>{country.name}</p>}

                        <img key={index} src={country.flags.png} 
                                alt="flag" height={150}
                                style={{margin: '10px',border: '1px solid gray'}}/>
                        
                    </div>
                ))}
        </div>
    );

}

我在App.js中是这样调用的:

<>
...
  <SearchForm />
</>

页面最初加载时可以显示CountrySWG元素列表,然后当我单击Search按钮时,我得到以下错误:

Uncaught TypeError: e.target is not a function
    at handleSubmit (SearchForm.js:13:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1).....

我需要做些什么才能在我按下搜索按钮时更新&lt;CountrySWG?

推荐答案

看起来您的代码中有一个小错误.调用e.Target时,参数应该是要作为目标的表单元素的名称或ID,而不是字符串"Query".此外,您应该使用e.Target.Value来获取输入字段的值.以下是您的代码的更正版本:

 const handleSubmit = (e:any) => {
    e.preventDefault();
    // in here query is a id of input which located in form
      console.log(e.target.query.value)
}
<form onSubmit={handleSubmit}>
  <input id='query' type="text" />
  <button type='submit'>submit</button>
</form>

enter image description here

Javascript相关问答推荐

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

通过嵌套模型对象进行Mongoose搜索

Spring boot JSON解析错误:意外字符错误

如何在 cypress 中使用静态嵌套循环

在Three JS中看不到补间不透明度更改效果

在FAQ Accodion应用程序中使用React useState时出现问题

按什么顺序接收`storage`事件?

匹配一个或多个可选重复的特定模式

FileReader()不能处理Firefox和GiB文件

如何使用Astro优化大图像?

如何找到带有特定文本和测试ID的div?

在渲染turbo流之后滚动到元素

有没有办法通过使用不同数组中的值进行排序

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

在ChartJS中使用spanGaps时,是否获取空值的坐标?

如何压缩图像并将其编码为文本?

当S点击按钮时,我如何才能改变它的样式?

MUI-TABLE:MUI表组件中交替行的不同 colored颜色 不起作用

如何在单击链接时设置一个JavaScript变量(以打开弹出窗口的特定部分)

带有JS模块模式的Rails的Importmap错误:";Net::ERR_ABORTED 404(未找到)&Quot;