简体   繁体   English

在 React 上实现 react-notification-system

[英]Implementing react-notification-system on React

I am trying to implement the react-notification-system: https://github.com/igorprado/react-notification-system but unable to get it to work.我正在尝试实施 react-notification-system: https : //github.com/igorprado/react-notification-system,但无法使其正常工作。 My code:我的代码:

// on the view

constructor() {
    super();
    this.state = {};
    this.state._notificationSystem = null;
    this.state = getProfileState();
}

componentDidMount() {
    TimelineStore.addChangeListener(this._onChange.bind(this));
    this._notificationSystem = this.refs.notificationSystem;
}

render () {
    <NotificationSystem ref="notificationSystem" />
}

// on the actions

createEvent(e) {
    e.preventDefault();
    this.props._notificationSystem.addNotification({
      message: 'Notification message',
      level: 'success'
    });
    this.props.closeModal();
}

I a getting the error:我收到错误:

Uncaught TypeError: Cannot read property 'addNotification' of undefined

I am guessing it is a problem with scope but could not figure out how to fix it.我猜这是范围的问题,但无法弄清楚如何解决它。

I couldn't understand your example.我无法理解你的例子。 The action mentioned is defined in the same component that you are rendering the NotificationSystem?提到的操作是在呈现 NotificationSystem 的同一组件中定义的? Or are you trying to pass the instance of NotificationSystem to another component via props?或者您是否试图通过道具将 NotificationSystem 的实例传递给另一个组件?

Well, if the view and action is in the same component, on your action you need to call like this:好吧,如果视图和动作在同一个组件中,你需要像这样调用你的动作:

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

Now, if you are trying to pass it via props to another component, you need to pass a function, like this:现在,如果您尝试通过 props 将其传递给另一个组件,则需要传递一个函数,如下所示:

// view

_notificationSystem = null

_getNotificationSystemInstance() {
  return this._notificationSystem
}

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

render() {
  return (
    <div>
      <EventComponent notification={ this._getNotificationSystemInstance } />
      <NotificationSystem ref="notificationSystem" />
    </div>
  );
}

// event component
createEvent(e) {
  e.preventDefault();
  this.props.notification().addNotification({
    message: 'Notification message',
    level: 'success'
  });
  this.props.closeModal();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM