在react / typescript中添加@ types / react-notification-system的问题

时间:2019-03-05 13:19:32

标签: reactjs typescript react-notifications

我在React + Javascript中使用了react-notification-system,当我在React + Typescript中尝试时,它会抛出

错误
Type 'ReactInstance' is not assignable to type 'null'
Type 'Element' is not assignable to type 'null'

componentDidMount()

中包含的行中

Patientadd.tsx

import * as NotificationSystem from 'react-notification-system';
class Patientadd extends React.Component<any, IState> {

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

  _notificationSystem: null;
  _addNotification=(event)=> {
      this._notificationSystem.addNotification({
        message: event.message,
        title:event.title,
        level: event.level,
        autoDismiss: event.autoDismiss,
      });
  }
  public render() {
    return (
        <div className="App2">
          <NotificationSystem ref="notificationSystem" />
        </div>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

请尝试

import React from 'react';
import ReactDOM from 'react-dom';
import * as NotificationSystem from 'react-notification-system';

export default class Patientadd extends React.Component<any, IState> {
  notificationSystem = React.createRef();

  addNotification = (event: any) => {
    event.preventDefault();
    const notification = this.notificationSystem.current;
    notification.addNotification({
      message: event.message,
      title:event.title,
      level: event.level,
      autoDismiss: event.autoDismiss,
    });
  };

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