2016-10-17 19 views
1

現在、反応通知を実装していますが、_addNotificationに渡されるイベントが定義されていないことを意味するCannot read property 'preventDefault' of undefinedというエラーが発生しています。反応通知を見つけることができません

_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

問題ここでは、それがeventのparamを期待しています、あなたが任意のパラメータなし_addNotificationを呼び出していることを、この機能です。

それはあなたがその場合は、あなたが、その場合の eventオブジェクトを持っていないので、あなたはどちらか event.preventDefault()を削除するか、 eventpreventDefault関数を呼び出す前に存在するかどうかを確認、APIサービスからデータをロードしているように私には見えます

。あなたがボタンやリンクから_addNotificationを呼び出している場合は

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

、あなたはpreventDefaultを呼び出す必要があります。したがって、私はそれを削除する代わりに、チェックを追加することをお勧めします。

幸運を祈る!

+0

ありがとうございました!これは問題を解決しますが、 'Can not read property 'のaddNotification' of null'というフォローアップの問題を提示します。これは、null _notificationSystemに通知を追加しないことを意味します。しかし、これはドキュメンテーション[ここ](https://github.com/igorprado/react-notification-system#creating-a-notification)に反するようです。これも迅速な修正であるのか、それとも自分自身の質問に値するのかはっきりしない。 –

関連する問題