我有一个使用 fetch API 获取用户数据的对象数组。我试过构造函数,创建一个函数,绑定它。它没有用。我尝试了 ComponentDidMount 和 setState,它返回未定义。
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}
/>
);
}
}
将状态值设置为数组并呈现它的正确方法是什么?谢谢