2 回答
TA貢獻1853條經驗 獲得超18個贊
我建議使用Promise
this.state = {
selectedDates: [Date],
currentType: null,
}
<button
onClick={() => {
this.props.onCreateClick(this.state.selectedDate).then(types => {
let newType = types[types.length - 1];
this.setState({
selectedDates: [],
currentType: newType.id
});
}).catch(error => {
//handle error
});
}}
>Create</button>
和onCreateClick函數返回無極其中reslove您收到新的類型,然后你處理它們在你的狀態組件。
如果您希望可以將此代碼更改為async\await,則與 promise 相同。
TA貢獻1906條經驗 獲得超10個贊
我會返回類似布爾值的內容onCreateClick()并將其存儲在變量中。然后您可以輕松檢查該變量是否已設置。
this.state = {
selectedDates: [Date],
currentType: null,
}
handleOnClick = () => {
const isHandled = this.props.onCreateClick(this.state.selectedDate)
if(isHandled){
let newType = this.props.types[this.props.types.length - 1];
this.setState({
selectedDates: [],
currentType: newType.id
})
}
}
<button type="button" onClick={() => this.handleOnClick()}>Create</button>
添加回答
舉報
