【发布时间】:2018-01-24 13:43:32
【问题描述】:
我有一个使用 fetch API 获取用户数据的对象数组。我试过构造函数,创建一个函数,绑定它。它没有用。我试过 ComponentDidMount 和 setState,它返回 undefined。
class Admin extends Component {
componentDidMount () {
var that = this;
fetch('http://localhost:4500/data/users', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}).then(function(response) {
return response.json();
}).then(function(json){
console.log(json);
that.state = {users: json};
});
}
render() {
return (
<SpicyDatatable
tableKey={key}
columns={columns}
rows={this.state.users}
config={customOptions}
/>
);
}
}
将状态值设置为数组并渲染它的正确方法是什么?谢谢
【问题讨论】:
标签: javascript node.js reactjs fetch-api