我现在已经进入第三周尝试简单地从 Express API 获取 json 响应到 React 应用程序中。我已经尝试了至少 40 个小时的教程,但我仍然无法让它工作。出于绝望,我想把它贴在这里,我知道我会被处以私刑,因为这将是某种形式的重复,但我正在寻找一个人来解决这个问题,希望能帮助我理解我做错了什么。
这是我从 React 调用它的最新尝试
import React, { Component } from 'react';
class App extends Component {
constructor(){
super();
this.state ={key: 0, title: ''};
}
componentDidMount() {
fetch('http://localhost:3001/')
.then(res => {
console.log(res);
return res.json()
})
.then(todos => {
console.log(todos);
this.setState({ todos })
});
}
render() {
return (
<div className="App">
<h1>todos</h1>
{this.state.todos.map(todo =>
<div key = {this.state.key} > title: {this.state.title} </div>
)}
</div>
);
}
}
export default App;
这会产生一个错误:Uncaught TypeError: Cannot read property 'map' of undefined
有人可以解释一下获取这种格式的数据的正确方法吗?
芜湖不芜
米琪卡哇伊
小唯快跑啊
相关分类