![](/img/trans.png)
[英]props not working inside normal javascript function .Cannot read property 'props' of undefined
[英]Calling props function inside component's function “Cannot read property 'props' of undefined”
我有一個按鈕可以觸發我的onClick函數:
<button id="closeModal" variant="danger" onClick={ this.onClick }></button>
我的onClick綁定在構造函數中:
this.onClick = this.handleSubmit.bind(this);
然后,我指定要調用兩個函數的函數:其中一個來自props,另一個不是(這是Formik的函數)。
onClick(event) {
this.handleSubmit.bind(this, values);
this.props.closeModal;
}
當我將它們切換到位置時,對於onClick中的兩個函數來說,錯誤Uncaught TypeError: Cannot read property '...' of undefined
相同。 我希望按鈕現在觸發兩個功能。 以前,當我只有
<button id="closeModal" variant="danger" onClick={ this.props.closeModal }></button>
一切都很好。 您知道如何解決此問題嗎?
我認為您沒有根據您的代碼綁定正確的功能,我希望看到這樣的內容
this.onClick = this.onClick.bind(this)
代替
this.onClick = this.handleSubmit.bind(this)
另外,您需要調用closeModal
因此應該
this.props.closeModal();
更改構造函數,然后檢查...
從
this.onClick = this.handleSubmit.bind(this);
至
this.onClick = this.onClick.bind(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.