我有一个使用抓取API获取用户数据的对象数组。我尝试过构造函数,创建一个函数,将其绑定。它没有工作。我试过ComponentDidMount和setState,它返回undefined。ReactJS:将状态值设置为数组的正确方法是什么?
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}
/>
);
}
}
将状态值设置为数组并将其呈现的正确方法是什么?谢谢
[reactjs如何更新状态]可能的复制(https://stackoverflow.com/questions/36527426/reactjs-how-to-update-state) –