我是 React 新手,正在尝试学习它。我正在从 API 获取数据并且我将使用这些数据。它返回基于“美元”的汇率。我将使用该数据来兑换货币,但我收到此错误:此处错误
我不知道问题是什么。
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor (props) {
super(props)
this.state = {data: 'false'};
}
componentDidMount(){
this.getData();
}
getData = () => {
fetch("https://openexchangerates.org/api/latest.json?app_id=88a3d2b24b174bf5bec485533a3bca88")
.then(response => {
if (response.ok) {
return response;
} else {
let errorMessage =
'${response.status(${response.statusText})',
error = new Error(errorMessage);
throw(error);
}
})
.then(response => response.json())
.then(json =>{
console.log(json);
this.setState({ data: json.data })
});
}
render() {
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">React App</h1>
</header>
{
this.state.data &&
this.state.data.map( (item, key) =>
<div key={key}>
{item}
</div>
)}
</div>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间。
将状态中的初始数据属性设置为空数组[]。字符串化的“false”计算结果为 true,这就是它尝试调用映射函数的原因,但字符串没有映射函数。
constructor (props) {
super(props)
this.state = {data: []};
}
Run Code Online (Sandbox Code Playgroud)
工作示例在这里
| 归档时间: |
|
| 查看次数: |
15103 次 |
| 最近记录: |