class Assets extends React.Component{
constructor() {
super();
this.state = {
assets: [],
secondsElapsed: 0
};
}
tick() {
if(start === true){
fetch('/api/user/all/?name='+name, {credentials: 'include', method: 'get'}).then(function(data){
return data.json();
}).then(json => {
this.setState({
assets: json
});
});
}
}
}
ReactDOM.render(<Assets />, document.getElementById('alerts'));
此代碼是引發此錯誤:的setState不工作
Uncaught (in promise) TypeError: Cannot read property 'setState' of null
at eval (eval at <anonymous> (bundleAlerts.js:3430), <anonymous>:265:15)
at <anonymous>
的代碼被遵守的WebPack如果有差別。我之前使用過ReactCreate類,它工作的很好,但我改變了它,試圖修復另一個錯誤。
謝謝,編輯。
我想結合的問題,使用此:'然後((數據)=> {...' –
沒錯的'then..'部分躲藏的地方。 –
我真的很傻,但我用'then((data)=> {....' –