1 回答

TA貢獻(xiàn)1805條經(jīng)驗 獲得超10個贊
你可以嘗試這樣的事情:
class PersonManager extends Component {
constructor(props) {
super(props);
this.state = {entities: []};
}
componentDidMount() {
const url = 'http://localhost:8080/bbstats/ws/person/findall';
fetch(url)
.then((response) => response.json())
.then((data) => {
this.setState({entities: data});
})
.catch(console.log);
}
render() {
return (
<div style={{maxWidth: 1440, marginLeft: 'auto', marginRight: 'auto'}}>
<DataTable value={this.state.entities}>
<Column field="id" header="ID" />
<Column field="lastName" header="Last Name" />
<Column field="firstName" header="First Name" />
<Column field="incognito" header="Incognito" />
</DataTable>
</div>
);
}
}
export default PersonManager;
所以我們可以entities在組件的狀態(tài)中保存PersonManager一個初始值。由于構(gòu)造函數(shù)在組件安裝和渲染之前運行,我們可以安全地訪問this.state.entities內(nèi)部render。
在基于類的組件中,我們可以通過調(diào)用重新渲染組件來更新部分狀態(tài)(entities在本例中) 。this.setState這樣組件就可以正確反映組件的當(dāng)前狀態(tài)。
添加回答
舉報