Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property '_notificationSystem' of null #128

Open
dparkar opened this issue Oct 31, 2017 · 12 comments
Open

Cannot read property '_notificationSystem' of null #128

dparkar opened this issue Oct 31, 2017 · 12 comments

Comments

@dparkar
Copy link

dparkar commented Oct 31, 2017

(new to React)

I am seeing the following error :

Uncaught TypeError: Cannot read property '_notificationSystem' of null
at _addNotification (notifier.jsx:11)
at Object.ReactErrorUtils.invokeGuardedCallback (ReactErrorUtils.js:69)
at executeDispatch (EventPluginUtils.js:85)
at Object.executeDispatchesInOrder (EventPluginUtils.js:108)
at executeDispatchesAndRelease (EventPluginHub.js:43)
at executeDispatchesAndReleaseTopLevel (EventPluginHub.js:54)
at Array.forEach ()
at forEachAccumulated (forEachAccumulated.js:24)
at Object.processEventQueue (EventPluginHub.js:254)
at runEventQueueInBatch (ReactEventEmitterMixin.js:17)
at Object.handleTopLevel [as _handleTopLevel] (ReactEventEmitterMixin.js:27)
at handleTopLevelImpl (ReactEventListener.js:72)
at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
at Object.batchedUpdates (ReactUpdates.js:97)
at dispatchEvent (ReactEventListener.js:147)

image

Here's how my code looks :

notifier.jsx :

import React, {Component} from 'react';
import NotificationSystem from 'react-notification-system';

export default class Notifier extends Component {
    
    _notificationSystem = null;

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

    componentDidMount() {
        this._notificationSystem = this.refs.notificationSystem;
      }

    render() {
        return (
            <div>
              <button onClick={this._addNotification}>Add notification</button>
              <NotificationSystem ref="notificationSystem" />
            </div>
            );
    }
}

elsewhere in component foo.jsx :

render() {
   return (
      <div className="notifier">
        <Notifier/>
      </div>
    );
}

Versions :

"react": "^15.6.2",
"react-dom": "^15.6.1",
"react-notification-system": "^0.2.16",
"react-router-dom": "^4.2.2"

Thanks for the help !

@cmkshaw
Copy link

cmkshaw commented Oct 31, 2017

I'm getting the same error TypeError: Cannot read property 'addNotification' of null when trying to structure my notification system as a higher order component.

My jsx:

import React, {Component} from 'react';
import NotificationSystem from 'react-notification-system';

export default (Notification) => {
    return class extends Component {
        constructor(props) {
            super(props);

            this.notificationSystem = null;
            this.success = this.success.bind(this);
            this.error = this.error.bind(this);
        }

        success(message){
            const title = "Notice!", level = "success";
            this.notificationSystem.addNotification({ title, message, level});
        }

        error(message){
            const title = "Error!", level = "error";
            this.notificationSystem.addNotification({ title, message, level});
        }

        render() {
            const props = Object.assign({}, this.props, {
                success: this.success,
                error: this.error
            });
            return <Notification {...props} >
                        <NotificationSystem ref={n => this.notificationSystem = n} style={false}/>
                    </Notification>;
        }
    };
};

Is there something I'm missing?

@raptox
Copy link

raptox commented Nov 1, 2017

same, cannot get this to work ...

@warrenlalata
Copy link

same

@warrenlalata
Copy link

have you fixed this?

@raptox
Copy link

raptox commented Nov 4, 2017

I used this repo instead

@wardnep
Copy link

wardnep commented Nov 8, 2017

same!

@sycu
Copy link

sycu commented Nov 17, 2017

Same here, any update?

@heshamelmasry77
Copy link

it is really annoying people make things and post it and it never works 🈂️

@a-atalla
Copy link

use this.refs.notificationSystem instead of this._notificationSystem

// The component
<NotificationSystem ref="notificationSystem" />

//The event handler
this.refs.notificationSystem.addNotification({
      message: 'Notification message',
      level: 'success'
    })

@rowthan
Copy link

rowthan commented Dec 23, 2017

at the time of componentDidMount(){}, this.refs is empty.
so you can not get this.refs.x.
use this.refs.notificationSystem instead of this._notificationSystem

@gautham20
Copy link

gautham20 commented Feb 10, 2018

Tried all the suggested methods, didn't work, used this instead

@dyst5422
Copy link

#144 should address this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests