我有一個使用抓取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) –