如何在React.js中添加通知

huangapple go评论74阅读模式
英文:

How add notifications in react js

问题

如何在React JS的PWA中添加带有操作按钮的通知?给一些想法,我是React JS的新手。

我正在尝试在我的渐进式Web应用中使用React JS添加通知,但它只是显示弹出消息,而不提供与移动应用程序类似的通知行为。

英文:

how to add notification with action buttons in pwa of react js give some idea i am new to react js

I am trying to add notifications in my progressive web app using react js but it simply gives toast message it does not gives notification behavior just like mobile application gives.

答案1

得分: 1

你可以使用react-notifications库来为你的Web应用添加通知。以下是文档和npm包的链接

这里是检查通知的示例代码:

import React from 'react';
import {
  NotificationContainer,
  NotificationManager,
} from 'react-notifications';

class Notifications extends React.Component {
  createNotification = (type) => {
    return () => {
      switch (type) {
        case 'info':
          NotificationManager.info('信息消息');
          break;
        case 'success':
          NotificationManager.success('成功消息', '标题在这里');
          break;
        case 'warning':
          NotificationManager.warning(
            '警告消息',
            '3000毫秒后关闭',
            3000
          );
          break;
        case 'error':
          NotificationManager.error('错误消息', '点击我!', 5000, () => {
            alert('回调');
          });
          break;
      }
    };
  };

  render() {
    return (
      <div>
        <button
          className="btn btn-info"
          onClick={this.createNotification('info')}
        >
          信息
        </button>
        <hr />
        <button
          className="btn btn-success"
          onClick={this.createNotification('success')}
        >
          成功
        </button>
        <hr />
        <button
          className="btn btn-warning"
          onClick={this.createNotification('warning')}
        >
          警告
        </button>
        <hr />
        <button
          className="btn btn-danger"
          onClick={this.createNotification('error')}
        >
          错误
        </button>

        <NotificationContainer />
      </div>
    );
  }
}

export default Notifications;
英文:

you can use the react-notifications library for adding notifications to your web app. Here is the link for the documentation and the npm package.
Here is some example code for checking the notifcations.

import React from &#39;react&#39;;
import {
NotificationContainer,
NotificationManager,
} from &#39;react-notifications&#39;;
class Notifications extends React.Component {
createNotification = (type) =&gt; {
return () =&gt; {
switch (type) {
case &#39;info&#39;:
NotificationManager.info(&#39;Info message&#39;);
break;
case &#39;success&#39;:
NotificationManager.success(&#39;Success message&#39;, &#39;Title here&#39;);
break;
case &#39;warning&#39;:
NotificationManager.warning(
&#39;Warning message&#39;,
&#39;Close after 3000ms&#39;,
3000
);
break;
case &#39;error&#39;:
NotificationManager.error(&#39;Error message&#39;, &#39;Click me!&#39;, 5000, () =&gt; {
alert(&#39;callback&#39;);
});
break;
}
};
};
render() {
return (
&lt;div&gt;
&lt;button
className=&quot;btn btn-info&quot;
onClick={this.createNotification(&#39;info&#39;)}
&gt;
Info
&lt;/button&gt;
&lt;hr /&gt;
&lt;button
className=&quot;btn btn-success&quot;
onClick={this.createNotification(&#39;success&#39;)}
&gt;
Success
&lt;/button&gt;
&lt;hr /&gt;
&lt;button
className=&quot;btn btn-warning&quot;
onClick={this.createNotification(&#39;warning&#39;)}
&gt;
Warning
&lt;/button&gt;
&lt;hr /&gt;
&lt;button
className=&quot;btn btn-danger&quot;
onClick={this.createNotification(&#39;error&#39;)}
&gt;
Error
&lt;/button&gt;
&lt;NotificationContainer /&gt;
&lt;/div&gt;
);
}
}
export default Notifications;

huangapple
  • 本文由 发表于 2023年5月29日 21:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定