【发布时间】:2020-04-08 03:12:39
【问题描述】:
我正在尝试使用来自Open AQ 的数据制作显示所选国家/地区污染最严重的 10 个城市的应用程序。但是,我收到错误消息,上面写着TypeError: this.props.cities.map is not a function,我不知道应该如何解决它。我发布了整个代码,因为您可能会发现一些我什至没有考虑过的错误。
这是我的应用文件:
import React from 'react';
import './App.css';
import Input from './components/Input';
import CitiesList from './components/CitiesList';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
countryCode: '',
cities: []
};
this.onCountryChange = this.onCountryChange.bind(this);
this.airQuality = this.airQuality.bind(this);
}
onCountryChange = (event) => {
this.setState({
countryCode: event.target.value
})
};
airQuality = () => {
const URL = 'https://api.openaq.org/v1/measurements?country=${countryCode}&limit=10&order_by=value&sort=desc';
fetch(URL)
.then(response => {
if (response.ok) {
return response;
}
throw Error(response.status)
})
.then(response => response.json())
.then(data => {
this.setState({
cities: data.results
})
})
.catch(error => console.log(error))
}
render() {
return (
<div>
<Input onChange={this.onCountryChange}/>
<CitiesList cities={this.airQuality}/>
</div>
)
}
}
export default App;
输入文件:
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
const countries = [
{country: 'Poland', value: 'PL'},
{country: 'Germany', value: 'DE'},
{country: 'Spain', value: 'ES'},
{country: 'France', value: 'FR'}
]
class Input extends React.Component {
render() {
return (
<Autocomplete
id="combo-box"
options={countries}
getOptionLabel={option => option.country}
style={{ width: 300, marginTop: 10 }}
renderInput={params => (
<TextField {...params} label="Type a country" variant="outlined" fullWidth />
)}
/>
)
}
}
export default Input;
和CitiesList 我得到错误的地方:
import React from 'react';
class CitiesList extends React.Component {
render() {
const items = this.props.cities.map((city, i) => (
<div key={i}>
<h2>{city.name}</h2>
<p>
<div>
Parameter: {city.parameter}
</div>
<div>
Value: {city.value} {city.unit}
</div>
</p>
</div>
));
return (
<div>
{items}
</div>
)
}
}
export default CitiesList;
感谢您的帮助。
编辑: Akshit Mehra 和 Roman Unt 帮助了我很多,我解决了我的代码中的两个问题,但是仍然存在一些错误。当我选择国家并看到时,它仍然不显示城市,这是因为
countryCode 没有更新。函数onCountryChange 甚至没有触发,我意识到这是因为 onChange 事件处理程序应该在输入标签中,我把它放在名为 Input 的组件中。所以我把它移到了Autocomplete 组件,现在我有了:
在App.js
<Input onCountryChange={this.onCountryChange}/>
在Input.js
const Input = ({onCountryChange}) => (
<Autocomplete
id="combo-box"
options={countries}
getOptionLabel={option => option.country}
style={{ width: 300, marginTop: 10 }}
onChange={onCountryChange}
renderInput={params => (
<TextField {...params} label="Type a country" variant="outlined" fullWidth />
)}
/>
)
现在我在控制台中看到函数 onCountryChange 触发,但它仍然没有更新 countryCode(this.state.countryCode 为 0)。
你能帮忙吗?
【问题讨论】:
-
你为什么在这里通过获取方法?而不是通过获取的城市传递状态?
标签: reactjs api dictionary fetch typeerror