interface Alertprops {
showAlert?: showAlertType,
HideAlert?: () => void,
onClick?: () => void,
}
interface Alertstate {
alertMess?: string,
showAlert?: boolean,
alertType?: string,
showReload?: boolean,
}
class Alert extends Component<Alertprops,Alertstate> {
constructor(props:Alertprops) {
super(props)
this.state = {
alertMess:'',
}
}
alertBox() {
return (
<div className="inner-box">
{this.state.alertMess}
<br/>
{this.state.showReload ? (<a className="reload-link" onClick={()=>{window.location.reload()}}>Reload page</a>):(null)}
<a onClick={()=>{this.props.HideAlert()}} className="close-alert">X</a>
</div>
)
}
上的onClick = {()=> {this.props.HideAlert()}}我收到錯誤“無法呼叫物件這可能是‘未定義’”什么林做錯了什么?剛開始用TS。
uj5u.com熱心網友回復:
嗨,如果您將函式作為可以未定義的道具傳遞,您可以像這樣傳遞它
this.props.HideAlert?.()
只有當你把它作為道具時才能運行
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352866.html
