反應,fetch()開發的UI和我落得這樣做的:我真的需要使用forceUpdate與獲取API和我使用ReactJS
getOperatorsList: function (obj) {
fetch('http://x.x.x.x/operators.list',
{
method: 'GET',
credentials: 'include'
}
).then(function (response) {
return response.json()
}).then(function (json) {
if (json.statusCode === 3) {
cookieService.unsetCookie('sessId');
}
obj.setState({ data: json },() => obj.forceUpdate());
}).catch(function (ex) {
console.log('parsing failed', ex);
})
}
這就是所謂的在我的組件運營商,看起來像這
var Operators = React.createClass({
getInitialState: function() {
return {
data: [{ "data": "Loading" }]
}
},
componentDidMount: function() {
operatorsService.getOperatorsList(this);
},
render: function() {
return (
<div>
<Row >
<Col>
<DataTablesCustom data={this.state.data} />
</Col>
</Row>
</div>
);
}
});
我已經有了一看this question,並且代碼不會爲我工作。
這工作正常,但我真的需要使用forceUpdate()
還是我有辦法讓代碼「更清潔」?
編輯:有一個setState
在子組件中看起來像這個this.setState({stuff: stuff}, this.function()});
。在將setState
更改爲this.setState({stuff: stuff},() => this.function()});
後,我能夠刪除forceUpdate()
。
它的工作原理沒有? –
沒有它,我想要的數據沒有顯示頁面加載時。 – Harmeko
所以你有你的答案,你必須使用它;)(這段代碼看起來很清楚) –