Toast 未渲染(react-toastify 组件)

新手上路,请多包涵

我正在使用 react-toastify 我无法得到一个简单的 toast 来呈现……

 import React from "react";
import { toast } from 'react-toastify';

class MyView extends React.Component<{}, {}> {

    constructor() {
        super();
        this.state = {

        };
    }

    componentDidMount() {
        toast("Hello", { autoClose: false });
    }

    notify = () => toast("Hello", { autoClose: false });

    render() {
        return (
           <div>
             <button onClick={this.notify}>Notify</button>
           </div>
      )}
}

package.json(在“依赖”部分)

 "react": "^16.2.0",
"react-toastify": "^3.2.2"

如果我调试它,我会看到我的 toast 已排队,_EventManager2 永远不会获得通常从队列中发出 toast 的 _constant.ACTION.MOUNTED 事件……

 /**
 * Wait until the ToastContainer is mounted to dispatch the toast
 * and attach isActive method
 */
_EventManager2.default.on(_constant.ACTION.MOUNTED, function (containerInstance) {
  container = containerInstance;

  toaster.isActive = function (id) {
    return container.isToastActive(id);
  };

  queue.forEach(function (item) {
    _EventManager2.default.emit(item.action, item.content, item.options);
  });
  queue = [];
});

..所以那个 ToastContainer 可能有问题但是什么?我只是使用文档中的示例代码。

谢谢您的帮助!

原文由 kevinob 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 597
1 个回答

您还必须 import 'react-toastify/dist/ReactToastify.css';

   import React, { Component } from 'react';
  import { ToastContainer, toast } from 'react-toastify';
  import 'react-toastify/dist/ReactToastify.css';
  // minified version is also included
  // import 'react-toastify/dist/ReactToastify.min.css';

  class App extends Component {
    notify = () => toast("Wow so easy !");

    render(){
      return (
        <div>
        <button onClick={this.notify}>Notify !</button>
          <ToastContainer />
        </div>
      );
    }
  }

原文由 Karan Shaw 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进