2016-10-17 16 views
1

當前正在執行響應通知,但遇到一個錯誤,表示Cannot read property 'preventDefault' of undefined,我認爲這意味着傳入_addNotification的事件未定義。無法找到響應通知

_handleSuccess: function(data, status, jqXHR) { 
    this._addNotification() 
    this.setState({ 
    errors: {}, 
    loading: false 
    }); 
}, 
_notificationSystem: null, 

_addNotification: function(event) { 
    event.preventDefault() 
    this._notificationSystem.addNotification({ 
    message: 'Notification message', 
    level: 'success', 
    position: 'bc' 
    }); 
}, 

_componentDidMount: function() { 
    this._notificationSystem = this.refs.notificationSystem; 
}, 

_getNotificationSystemInstance: function() { 
    return this 
}, 

回答

2

這裏的問題是,你在呼喚_addNotification不帶任何參數,這個功能它是在等一個event PARAM。

它看起來對我說,你是從API服務加載數據,如果是那樣的話,你不具備這種情況下,event對象,所以你要麼刪除event.preventDefault()或檢查event調用preventDefault函數之前存在。

_addNotification: function(event) { 
     event && event.preventDefault(); // <--- Check if event exist! 
     this._notificationSystem.addNotification({ 
     message: 'Notification message', 
     level: 'success', 
     position: 'bc' 
     }); 
}, 

在您從一個按鈕或鏈接調用_addNotification的情況下,你確實需要調用preventDefault。因此,我會建議添加支票而不是刪除它。

祝你好運!

+0

謝謝!這確實解決了這個問題,但是它提出了'Can not read property'addNotification'null'的後續問題,這意味着它不會向null _notificationSystem添加通知。但是,這似乎與文檔[此處](https://github.com/igorprado/react-notification-system#creating-a-notification)相反。不知道這是否也是一個快速修復或值得自己的問題。 –