【发布时间】:2020-06-02 17:39:30
【问题描述】:
我正在编写一个应用程序(从全栈打开),它将搜索国家,如果只选择一个国家,则显示国家信息。
该应用程序在人类使用时运行良好,但是,如果显示超过 1 个国家/地区,我希望用户能够单击国家名称旁边的“显示”按钮,并显示给定国家/地区的国家信息。
我想我可以创建一个函数来更改搜索字段的值,并将该值设置为给定国家/地区的名称。
我的问题是,输入上的 onChange 在以编程方式填写时不会触发。
我尝试为输入字段创建一个新的钩子,但我也无法让它工作。 当以编程方式填充该字段时,如何触发我的 onChange 触发器?
我想在函数toggleShowCountry中做到这一点
代码:
import React, {useState, useEffect} from 'react';
import axios from 'axios';
function App() {
const [countries, setCountries] = useState([]);
const [search, setSearch] = useState(countries);
const [inputField, setInputField] = useState("");
const fetchCountries = () => {
axios
.get('https://restcountries.eu/rest/v2/all')
.then(response => {
setCountries(response.data);
});
};
useEffect(fetchCountries, []);
const handleSearch = (e) => {
e.preventDefault();
const searchField = document.querySelector('#searchCountry').value;
setSearch(Object.values(countries).filter(country =>
country.name.toLowerCase().includes(searchField.toLowerCase())))
}
const toggleShowCountry = (props) => {
const searchField = document.querySelector('#searchCountry');
searchField.value = props;
}
return (
<div>
<h2>Search countries</h2>
<Search handleSearch={handleSearch} />
<Display countries={search} toggleShowCountry={toggleShowCountry} />
</div>
)
}
const Display = (props) => {
const countries = props.countries;
let filteredCountries = countries.map(country => country);
if (countries.length > 10){
return(
<>
<p>Too many matches, specify another filter.</p>
</>
)
} else if (countries.length === 1){
return (
<>
{filteredCountries.map(country =>
<div key={country}>
<ShowCountry country={country} key={country} />
</div>
)}
</>
)
} else {
return (
<>
{filteredCountries.map(country =>
<ul key={country.name}>
<FilteredCountries country={country} key={country.name} toggleShowCountry={props.toggleShowCountry}/>
</ul>
)}
</>
)
}
}
const FilteredCountries = (props) => {
const country = props.country;
return (
<>
<li>{country.name} <button onClick={() => props.toggleShowCountry(country.name)}>Show</button></li>
</>
)
}
const ShowCountry = ({country}) => {
return (
<>
<h2>{country.name}</h2>
<p>Capital: {country.capital}</p>
<p>Population: {country.population}</p>
<h3><strong>Languages</strong></h3>
<ul>
{country.languages.map(language =>
<li key={language.name}>{language.name}</li>
)}
</ul>
<img src={country.flag} alt="flag" width="100px"/>
</>
)
}
const Search = (props) => {
return (
<div>
<form>
<div>
<label htmlFor="countries">Enter country </label>
<input name="countries" id="searchCountry" onChange={props.handleSearch} />
</div>
</form>
</div>
)
}
export default App;
【问题讨论】:
标签: javascript reactjs