问题描述
这是我在Codesandbox上的应用程序的链接 https://codesandbox.io/s/filter-search-gxidc?file =/src/App.js 这只是一个组件app.js
Here is a link to my app on Codesandbox https://codesandbox.io/s/filter-search-gxidc?file=/src/App.js it's only one component app.js
在data.js中,我有一个对象数组:
In data.js I have an array of objects:
export const data = [
{
currencies: [{ code: "AFN", name: "Afghan afghani", symbol: "؋" }],
languages: [
{ iso639_1: "ps", iso639_2: "pus", name: "Pashto", nativeName: "پښتو" },
{ iso639_1: "uz", iso639_2: "uzb", name: "Uzbek", nativeName: "Oʻzbek" },
{
iso639_1: "tk",
iso639_2: "tuk",
name: "Turkmen",
nativeName: "Türkmen"
}
],
flag: "https://restcountries.eu/data/afg.svg",
name: "Afghanistan",
topLevelDomain: [".af"],
capital: "Kabul",
region: "Asia",
subregion: "Southern Asia",
population: 27657145,
borders: ["IRN", "PAK", "TKM", "UZB", "TJK", "CHN"],
nativeName: "افغانستان"
},
{
currencies: [{ code: "EUR", name: "Euro", symbol: "€" }],
languages: [
{
iso639_1: "sv",
iso639_2: "swe",
name: "Swedish",
nativeName: "svenska"
}
],
flag: "https://restcountries.eu/data/ala.svg",
name: "Åland Islands",
topLevelDomain: [".ax"],
capital: "Mariehamn",
region: "Europe",
subregion: "Northern Europe",
population: 28875,
borders: [],
nativeName: "Åland"
},
{
currencies: [{ code: "ALL", name: "Albanian lek", symbol: "L" }],
languages: [
{ iso639_1: "sq", iso639_2: "sqi", name: "Albanian", nativeName: "Shqip" }
],
flag: "https://restcountries.eu/data/alb.svg",
name: "Albania",
topLevelDomain: [".al"],
capital: "Tirana",
region: "Europe",
subregion: "Southern Europe",
population: 2886026,
borders: ["MNE", "GRC", "MKD", "KOS"],
nativeName: "Shqipëria"
},
{
currencies: [{ code: "DZD", name: "Algerian dinar", symbol: "د.ج" }],
languages: [
{ iso639_1: "ar", iso639_2: "ara", name: "Arabic", nativeName: "العربية" }
],
flag: "https://restcountries.eu/data/dza.svg",
name: "Algeria",
topLevelDomain: [".dz"],
capital: "Algiers",
region: "Africa",
subregion: "Northern Africa",
population: 40400000,
borders: ["TUN", "LBY", "NER", "ESH", "MRT", "MLI", "MAR"],
nativeName: "الجزائر"
},
{
currencies: [{ code: "USD", name: "United State Dollar", symbol: "$" }],
languages: [
{
iso639_1: "en",
iso639_2: "eng",
name: "English",
nativeName: "English"
},
{
iso639_1: "sm",
iso639_2: "smo",
name: "Samoan",
nativeName: "gagana fa'a Samoa"
}
],
flag: "https://restcountries.eu/data/asm.svg",
name: "American Samoa",
topLevelDomain: [".as"],
capital: "Pago Pago",
region: "Oceania",
subregion: "Polynesia",
population: 57100,
borders: [],
nativeName: "American Samoa"
},
{
currencies: [{ code: "EUR", name: "Euro", symbol: "€" }],
languages: [
{ iso639_1: "ca", iso639_2: "cat", name: "Catalan", nativeName: "català" }
],
flag: "https://restcountries.eu/data/and.svg",
name: "Andorra",
topLevelDomain: [".ad"],
capital: "Andorra la Vella",
region: "Europe",
subregion: "Southern Europe",
population: 78014,
borders: ["FRA", "ESP"],
nativeName: "Andorra"
},
{
currencies: [{ code: "AOA", name: "Angolan kwanza", symbol: "Kz" }],
languages: [
{
iso639_1: "pt",
iso639_2: "por",
name: "Portuguese",
nativeName: "Português"
}
],
flag: "https://restcountries.eu/data/ago.svg",
name: "Angola",
topLevelDomain: [".ao"],
capital: "Luanda",
region: "Africa",
subregion: "Middle Africa",
population: 25868000,
borders: ["COG", "COD", "ZMB", "NAM"],
nativeName: "Angola"
}
];
我想遍历它,并在app.js上显示每个国家的信息(我做了这部分)
I wanna map through it and display information about each country (I did this part ) at app.js
这是app.js中的代码
here is the code in app.js
import React, { useState, useEffect } from "react";
import "./styles.css";
import { data } from "./data";
export default function App() {
const [cuntries, setCountries] = useState([]);
const [region, setRegion] = useState('');
useEffect(()=>{
setCountries(data)
},[])
function filterByRegion(event) {
setRegion(event.target.value);
handleSubmit()
// event.preventDefault()
}
function handleSubmit(){
const newArr = cuntries.filter(cunt=> cunt.region === region)
setCountries(newArr)
}
console.log(region);
console.log(cuntries);
return (
<div className="App">
<h1>filtering</h1>
<form onChange={filterByRegion}>
<label>Filter by region:</label>
<select name="regions" id="region-select" >
<option value="">Chose a region</option>
<option value="Africa">Africa</option>
<option value="Americas">Americas</option>
<option value="Asia">Asia</option>
<option value="Oceania">Oceania</option>
<option value="Polar">Polar</option>
</select>
</form>
<div>
{cuntries.map((country) => {
return (
<div key={country.name} className="country">
<h3>{country.name}</h3>
<h3>{country.nativeName}</h3>
<h3>{country.region}</h3>
</div>
);
})}
</div>
</div>
);
}
在app.js中,我有一个包含区域的select元素我想过滤用于渲染每个国家/地区的数组,以便当我选择其他地区时,地图方法会渲染该地区中的国家/地区
In app.js I have a select element that contains regionsI wanna filter the array that I used to render each country So that when I choose a different region the map method render the countries that in this region
我尝试了一些解决方案,但没有一个起作用.
I tried some solution but non of them worked.
再次链接到应用程序 https://codesandbox.io/s/filter-search-gxidc?file=/src/App.js:0-1424
the link to the app again https://codesandbox.io/s/filter-search-gxidc?file=/src/App.js:0-1424
推荐答案
主要问题可能是 setRegion
是异步处理的,因此一旦您尝试通过 handleSubmit 中的值进一步过滤代码>,则该值很可能不存在.请在下面查看对组件的建议修改.
The main problem could be
setRegion
is handled asynchronously so once you try to filter further by its value in handleSubmit
then the value is most probably not there. See down below the suggested modifications on your component.
第一件事是将
onChange
事件添加到 select
而不是 form
:
The first thing is to add
onChange
event to select
instead of form
:
<select name="regions" id="region-select" onChange={filterByRegion}>
<option value="">Chose a region</option>
<option value="Africa">Africa</option>
<option value="Americas">Americas</option>
<option value="Asia">Asia</option>
<option value="Oceania">Oceania</option>
<option value="Polar">Polar</option>
</select>
修改
filterRegion
,将值向下传递给 handleSubmit
:
Modify
filterRegion
to pass down the value to handleSubmit
as:
const filterByRegion = (event) => {
const region = event.target.value
// I would keep this state if you really need for other purposes
setRegion(region)
handleSubmit(region)
}
然后,我将使用
.filter
data
作为原始数组,以查找值:
Then I would use with
.filter
the data
as the original array to find values as:
const handleSubmit = (regionValue) => {
const newArr = data.filter(c => c.region === regionValue)
setCountries(newArr)
}
Please find the working CodeSandbox here.
If you want to render something for no values found state for you countries:
{cuntries && cuntries.length > 0 ?
cuntries.map((country) => {
return (
<div key={country.name} className="country">
<h3>{country.name}</h3>
<h3>{country.nativeName}</h3>
<h3>{country.region}</h3>
</div>
);
})
: <div>No value found</div>
}
因此,如果您的
countries
数组中没有值,则可以呈现一个< div>
,指出没有找到任何值.
这篇关于通过选择元素的值过滤组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!