我正在尝试在标准 Fluxible 项目中使用此组件https://github.com/igorprado/react-notification-system,并正在寻找有关如何将示例代码调整为 es6 样式类的指导。
这是原始示例代码:
var React = require('react');
var ReactDOM = require('react-dom');
var NotificationSystem = require('react-notification-system');
var MyComponent = React.createClass({
_notificationSystem: null,
_addNotification: function(event) {
event.preventDefault();
this._notificationSystem.addNotification({
message: 'Notification message',
level: 'success'
});
},
componentDidMount: function() {
this._notificationSystem = this.refs.notificationSystem;
},
render: function() {
return (
<div>
<button onClick={this._addNotification}>Add notification</button>
<NotificationSystem ref="notificationSystem" />
</div>
);
}
});
ReactDOM.render(
React.createElement(MyComponent),
document.getElementById('app')
);
这是我尝试将其添加到可流动应用程序组件中的尝试,我应该将 notificationSystem 对象添加到状态中吗?如果我连接到商店,使用 componentDidMount 是否总是可靠的?我应该如何从操作触发通知 - 我应该更新触发组件的 notificationStore 还是直接从操作本身以某种方式对组件进行操作?
class Application extends React.Component {
//constructor(props) {
// super(props);
// this.state = {
// notificationSystem: this.refs.notificationSystem
// };
//}
addNotification(event) {
event.preventDefault();
this.notificationSystem.addNotification({
message: 'Notification message',
level: 'success'
});
}
render() {
var Handler = this.props.currentRoute.get('handler');
return (
<div>
<Nav currentRoute={this.props.currentRoute} links={pages} />
<div className="main">
<Handler />
</div>
<NotificationSystem ref="notificationSystem" />
</div>
);
}
componentDidMount() {
this.state.notificationSystem = this.refs.notificationSystem;
}
componentDidUpdate(prevProps, prevState) {
const newProps = this.props;
if (newProps.pageTitle === prevProps.pageTitle) {
return;
}
document.title = newProps.pageTitle;
}
}