Javascript 当'countInfo'已经存在时,在下面的react.js代码中需要'details'做什么?
尽管Javascript 当'countInfo'已经存在时,在下面的react.js代码中需要'details'做什么?,javascript,html,json,reactjs,Javascript,Html,Json,Reactjs,尽管.json文件不包含任何键命名的详细信息,但有人能说出详细信息来自何处吗。但是调用countInfo而不是details也会抛出一个错误 import React, { useState } from 'react'; import DisplayData from './DisplayData'; const SearchCountry = () => { const [country, setCountry] = useState
.json
文件不包含任何键
命名的详细信息,但有人能说出详细信息来自何处吗。但是调用countInfo而不是details也会抛出一个错误
import React, { useState } from 'react';
import DisplayData from './DisplayData';
const SearchCountry = () => {
const [country, setCountry] = useState('');
const [details, setDetails] = useState({});
const [countInfo, setCountryInfo] = useState({})
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const search = async e => {
setLoading(true);
e.preventDefault();
const noCountry = () => {
return (
<div>
{alert("Can not search empty string")}
{setLoading(false)}
</div>
)
}
let url;
country === '' ? noCountry() : url = `https://corona.lmao.ninja/v2/countries/${country}`
try {
let res = await fetch(url);
let data = await res.json();
if (res.ok) {
setDetails(data)
setCountry("")
setCountryInfo({})
setLoading(false)
} else {
setError(true)
alert('ENTER THE CORRECT NAME')
setTimeout(' ',4000);
window.location.reload();
setCountry("")
}
const info = await data.countryInfo
setCountryInfo(info)
setLoading(false);
}
catch (err) {
alert(err);
}
}
return (
<>
<form className="form" onSubmit={search}>
<label htmlFor="country" className="label">COUNTRY</label>
<input
type="text"
name="country"
placeholder="Enter Country.."
className="input"
value={country}
onChange={e => setCountry(e.currentTarget.value)}
/>
<button
type="submit"
className="button"
>
Search
</button>
</form>
{
error ? <h3 className="error"/> : <DisplayData
imgSrc={countInfo.flag}
cases={details.cases}
deaths={details.deaths}
recovered={details.recovered}
todayCases={details.todayCases}
loading={loading}
/>
}
</>
)
}
export default SearchCountry;
import React,{useState}来自“React”;
从“./DisplayData”导入显示数据;
const SearchCountry=()=>{
const[country,setCountry]=useState(“”);
const[details,setDetails]=useState({});
const[countInfo,setCountryInfo]=useState({})
const[loading,setLoading]=useState(false);
const[error,setError]=useState(false);
常量搜索=异步e=>{
设置加载(真);
e、 预防默认值();
const noCountry=()=>{
返回(
{警报(“无法搜索空字符串”)}
{setLoading(false)}
)
}
让url;
国家==''?无国家():url=`https://corona.lmao.ninja/v2/countries/${country}`
试一试{
let res=等待获取(url);
让data=wait res.json();
如果(res.ok){
设置详细信息(数据)
国家(“”)
setCountryInfo({})
设置加载(错误)
}否则{
设置错误(真)
警报('输入正确的名称')
设置超时(“”,4000);
window.location.reload();
国家(“”)
}
const info=wait data.countryInfo
setCountryInfo(信息)
设置加载(假);
}
捕捉(错误){
警惕(err);
}
}
返回(
国家
setCountry(e.currentTarget.value)}
/>
搜寻
{
错误?:
}
)
}
出口国;
您在以下行设置了详细信息
状态:setDetails(data)
搜索代码中的“详细信息”一词是否不会向您透露?现在还不清楚你想问什么或者问题出在哪里。顺便说一下,这不是我的代码。我在问const[details,setDetails]=useState({})需要什么代码>行,而const[countInfo,setCountryInfo]=useState({})
存在。为什么当cases={details.cases}死亡={details.details}恢复={details.recovered}今天的cases={details.todayCases}时显示错误
当详细信息更改为CountInfo时,这会显示eror。如果这不是您的代码,并且您想知道它为什么以某种方式编写,那么您需要询问编写它的人。如果您想知道错误发生的原因,那么第一步就是读取错误消息。在调试器中单步执行代码也是观察其行为的有用方法。我们不知道这段代码的目的是什么,运行时数据是什么,代码是否工作,等等。