reactjs - 反应按钮以显示元素
问题描述
我正在从 Restcountries API 中提取国家,如果数组的当前状态有多个国家或少于或等于十个国家,我想列出国家名称以及每个国家旁边的“显示”按钮。显示按钮应显示我的Country
函数的返回(渲染)中的内容。在App
函数中,我为名为 的按钮编写了一个处理程序handleViewButton
。我对如何在条件语句中过滤Countries
函数中的元素以显示. 我尝试传递给该函数,但出现错误“未捕获的 TypeError:newSearch.toLowerCase 不是函数”。我真的只想触发该功能以显示按下的国家/地区按钮。else
Country
handleViewButton
Button
Country
应用程序.js
import React, { useState, useEffect } from 'react';
import './App.css';
import axios from 'axios';
const Country = ({country}) => {
return (
<>
<h2>{country.name}</h2>
<p>capital {country.capital}</p>
<p>population {country.population}</p>
<br/>
<h3>languages</h3>
{country.languages.map(language => <li key={language.name}>{language.name}</li>)}
<br/>
<img src={country.flag} alt="country flag" style={{ width: '250px'}}/>
</>
);
}
const Countries = ({countries, handleViewButton}) => {
const countriesLen = countries.length;
console.log(countriesLen)
if (countriesLen === 0) {
return (
<p>Please try another search...</p>
);
} else if (countriesLen === 1) {
return (
<ul>
{countries.map((country, i) => <Country key={i} countriesLen={countriesLen} country={country}/>)}
</ul>
);
} else if (countriesLen > 10) {
return (
<div>
<p>Too many matches, specify another filter</p>
</div>
);
} else {
return (
<ul>
{countries.map((country, i) => <li key={i}>{country.name}<Button handleViewButton={handleViewButton}/></li>)}
</ul>
)
};
};
const Button = ({handleViewButton}) => {
return (
<button onClick={handleViewButton}>Show</button>
);
};
const Input = ({newSearch, handleSearch}) => {
return (
<div>
find countries <input value={newSearch} onChange={handleSearch}/>
</div>
);
};
function App() {
const [countries, setCountries] = useState([]);
const [newSearch, setNewSearch] = useState('');
const handleSearch = (event) => {
const search = event.target.value;
setNewSearch(search);
};
const handleViewButton = (event) => {
const search = event.target.value;
setNewSearch(countries.filter(country => country === search));
};
const showCountrySearch = newSearch
? countries.filter(country => country.name.toLowerCase().includes(newSearch.toLowerCase()))
: countries;
useEffect(() => {
axios
.get('https://restcountries.eu/rest/v2/all')
.then(res => {
setCountries(res.data);
console.log('Countries array loaded');
})
.catch(error => {
console.log('Error: ', error);
})
}, []);
return (
<div>
<Input newSearch={newSearch} handleSearch={handleSearch}/>
<Countries countries={showCountrySearch} handleViewButton={handleViewButton}/>
</div>
);
};
export default App;
解决方案
您可以使用 adisplayCountry
来处理应显示的国家/地区。大多数情况下,您会使用 id,但我在这里使用country.name
它是因为它应该是唯一的。
然后,您将使用matchedCountry
查找您的国家/地区列表。
之后, aonHandleSelectCountry
选择一个给定的国家。如果它已经被选中,那么你可以设置null
为取消选择。
最后,您将有条件地呈现您的matchedCountry
:
const Countries = ({countries}) => {
const [displayCountry, setDisplayCountry] = useState(null);
const countriesLen = countries.length;
const matchedCountry = countries.find(({ name }) => name === displayCountry);
const onHandleSelectCountry = (country) => {
setDisplayCountry(selected => {
return selected !== country.name ? country.name : null
})
}
if (countriesLen === 0) {
return (
<p>Please try another search...</p>
);
} else if (countriesLen === 1) {
return (
<ul>
{countries.map((country, i) => <Country key={i} countriesLen={countriesLen} country={country}/>)}
</ul>
);
} else if (countriesLen > 10) {
return (
<div>
<p>Too many matches, specify another filter</p>
</div>
);
} else {
return (
<>
<ul>
{countries.map((country, i) => <li key={i}>{country.name}<Button handleViewButton={() => onHandleSelectCountry(country)}/></li>)}
</ul>
{ matchedCountry && <Country countriesLen={countriesLen} country={matchedCountry}/> }
</>
)
};
};
推荐阅读
- linux - Linux终端写入json但输入只产生一个新行如何运行命令
- decimal - QGIS不会将y和x坐标更改为十进制度
- python - 根据日期范围合并行
- pyspark - 找到“匹配”时打印上一行:Pyspark
- python - 拆分数据时出现问题:KeyError: "None of [Int64Index([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], dtype='int64')] 在[列]"
- node.js - TypeError: _babel_runtime_corejs2_core_js_promise__WEBPACK_IMPORTED_MODULE_17___default.a.allSettled 不是函数
- html - 响应式 iframe,但没有声音
- c# - .NET 4.6 中的 OpenIddict 3 不可用令牌验证
- wix - WiX 2716 错误:无法为组件创建随机子组件名称
- javascript - 如何将列表更改为具有布尔值的字典