首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Toast未呈现(react-toastify组件)

Toast未呈现(react-toastify组件)
EN

Stack Overflow用户
提问于 2018-03-20 15:42:11
回答 8查看 27.7K关注 0票数 20

我正在使用react-toastify,但我无法获得一个简单的吐司来呈现...

代码语言:javascript
复制
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 (在“依赖项”部分)

代码语言:javascript
复制
"react": "^16.2.0",
"react-toastify": "^3.2.2"

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

代码语言:javascript
复制
/**
 * 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 = [];
});

..so ToastContainer可能有点问题,但是什么问题呢?我只使用文档中的示例代码。

谢谢你的帮助!

EN

回答 8

Stack Overflow用户

发布于 2018-05-06 16:08:32

你还得import 'react-toastify/dist/ReactToastify.css';

代码语言:javascript
复制
  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>
      );
    }
  }
票数 47
EN

Stack Overflow用户

发布于 2019-05-03 09:08:32

在声明类之前,添加以下行:

代码语言:javascript
复制
toast.configure();
票数 15
EN

Stack Overflow用户

发布于 2020-02-27 02:20:12

其他解决方案对我不起作用--原来我没有向toast.*函数传递字符串。例如:

代码语言:javascript
复制
  getSomethingFromApi()
     .then((response) =>  { 
         // this works fine because response.message is a string
         toast.notify(response.message)
      })
     .catch((error) => {
         // this will fail to render because error is an object, not a string
         toast.error(error)
      })
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49378743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档