Here’s the code:
JavaScript
x
136
136
1
import axios from 'axios';
2
import React from 'react';
3
import { useState, useEffect } from 'react';
4
5
6
const FilterLengthZero = ({ setEmptyDetailedCountry }) => {
7
useEffect(() => setEmptyDetailedCountry(), [])
8
return <div>Type to search</div>
9
}
10
11
const FilterLengthNine = ({ setEmptyDetailedCountry }) => {
12
useEffect(() => setEmptyDetailedCountry(), [])
13
return <div>Too many matches. Type more!</div>
14
}
15
16
const FilterLengthGreaterThanOne = ({ filteredCountries, buttonHandler }) => {
17
return (
18
<div>
19
{
20
filteredCountries.map(country => (
21
<CountryButton key={country.name.common} country={country} onClick={() => buttonHandler(country)} />
22
))
23
}
24
</div>
25
)
26
}
27
28
const FilterLengthEqualToOne = ({ filteredCountries, setActualDetailedCountry }) => {
29
useEffect(() => setActualDetailedCountry(filteredCountries[0]), [])
30
}
31
32
const Display = ({ country }) => {
33
34
return (
35
<div>
36
<h2>Country</h2>
37
<div>Capital {country.capital}</div>
38
<div>Area {country.area}</div>
39
<h3>Languages</h3>
40
<ul>
41
{Object.values(country.languages).map(language => (
42
<li key={language}>{language}</li>
43
))}
44
</ul>
45
<img src={country.flags.png} />
46
</div>
47
);
48
};
49
50
const CountryButton = ({ country, onClick }) => {
51
return (
52
<div>
53
{country.name.common}
54
<button onClick={() => onClick(country)}>show</button>
55
</div>
56
);
57
};
58
59
const CheckConditions = ({ setEmptyDetailedCountry, filteredCountries, filter, buttonHandler, setActualDetailedCountry }) => {
60
if (filter === '') {
61
return <FilterLengthZero setEmptyDetailedCountry={setEmptyDetailedCountry} />
62
}
63
else if (filteredCountries.length > 9) {
64
return <FilterLengthNine setEmptyDetailedCountry={setEmptyDetailedCountry} />
65
}
66
else if (filteredCountries.length > 1) {
67
return <FilterLengthGreaterThanOne filteredCountries={filteredCountries} buttonHandler={buttonHandler} />
68
}
69
else if (filteredCountries.length === 1) {
70
return <FilterLengthEqualToOne filteredCountries={filteredCountries} setActualDetailedCountry={setActualDetailedCountry} />
71
}
72
else {
73
return <></>
74
}
75
}
76
77
const CheckDetailedDisplayConditions = ({ detailedCountry }) => {
78
if (detailedCountry.length != 1) {
79
console.log('no detailed country')
80
return <></>
81
}
82
else {
83
console.log('1 detailed country')
84
return <Display country={detailedCountry} />
85
}
86
}
87
88
const App = () => {
89
const [countries, setCountries] = useState([]);
90
const [filter, setFilter] = useState('');
91
const [detailedCountry, setDetailedCountry] = useState('');
92
93
useEffect(() => {
94
axios.get('https://restcountries.com/v3.1/all').then(response => {
95
setCountries(response.data);
96
});
97
}, []);
98
99
const buttonHandler = country => {
100
setDetailedCountry(country);
101
};
102
103
const filterHandler = event => {
104
setFilter(event.target.value);
105
};
106
107
const filterCountries = country => {
108
return country.name.common.toLowerCase().includes(filter.toLowerCase());
109
};
110
111
const countriesSubset = countries.filter(filterCountries);
112
113
function setEmptyDetailedCountry() {
114
setDetailedCountry('')
115
}
116
117
function setActualDetailedCountry(country) {
118
setDetailedCountry(country)
119
}
120
121
return (
122
<div>
123
find country: <input onChange={filterHandler} />
124
<CheckConditions
125
setEmptyDetailedCountry={setEmptyDetailedCountry}
126
filteredCountries={countriesSubset}
127
filter={filter}
128
buttonHandler={buttonHandler}
129
setActualDetailedCountry={setActualDetailedCountry} />
130
<CheckDetailedDisplayConditions detailedCountry={detailedCountry} />
131
</div>
132
);
133
};
134
135
export default App;
136
I’ve been trying to learn React using this online MOOC. I’m on exercise 2.12* Data for countries, step1 and I’m stuck. My search is running fine but I’m unable to solve the last part of the problem: printing information of a selected country or if only 1 country matches the search string.
I tried debugging through console.log and it turns out that whenever I’m calling the ‘setDetailedCountry’ method, it’s never working. My ‘DetailedCountry’ hook is always empty!
Please help me figure out what the problem is.
Advertisement
Answer
setDetailedCountry
works as intended
Your detailedCountry
doesn’t have property length
so in CheckDetailedDisplayConditions
detailedCountry.length != 1
always resolves as true
Maybe use detailedCountry
instead
JavaScript
1
11
11
1
const CheckDetailedDisplayConditions = ({ detailedCountry }) => {
2
if (!detailedCountry) {
3
console.log('no detailed country')
4
return <></>
5
}
6
else {
7
console.log('1 detailed country')
8
return <Display country={detailedCountry} />
9
}
10
}
11