我正在使用react-toastify,但我无法获得一个简单的吐司来呈现...
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 = [];
});..so ToastContainer可能有点问题,但是什么问题呢?我只使用文档中的示例代码。
谢谢你的帮助!
发布于 2018-05-06 16:08:32
你还得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>
);
}
}发布于 2019-05-03 09:08:32
在声明类之前,添加以下行:
toast.configure();发布于 2020-02-27 02:20:12
其他解决方案对我不起作用--原来我没有向toast.*函数传递字符串。例如:
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)
})https://stackoverflow.com/questions/49378743
复制相似问题