我正在使用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>
)}
}
Run Code Online (Sandbox Code Playgroud)
package.json(在"依赖项"部分中)
"react": "^16.2.0",
"react-toastify": "^3.2.2"
Run Code Online (Sandbox Code Playgroud)
如果我调试它,我看到我的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 = [];
});
Run Code Online (Sandbox Code Playgroud)
..那么ToastContainer可能有问题,但是什么?我只是使用文档中的示例代码.
谢谢您的帮助!
小智 21
你也必须
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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 14
在声明您的类之前,添加以下行:
toast.configure();
Run Code Online (Sandbox Code Playgroud)
小智 12
使用命令安装react-toastify
npm i react-toastify
Run Code Online (Sandbox Code Playgroud)
然后:
import {ToastContainer,toast} from 'react-toastify'
Run Code Online (Sandbox Code Playgroud)
并作为回报添加
<ToastContainer />
Run Code Online (Sandbox Code Playgroud)
之后当你这样做时toast('hy')
它会显示 toast
小智 9
更好的是,导入缩小的 css:
import 'react-toastify/dist/ReactToastify.min.css';
或者如果您将其导入到 .scss 文件中
@import '~react-toastify/dist/ReactToastify.min.css';
其他解决方案对我不起作用 - 结果我没有将字符串传递给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)
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7930 次 |
| 最近记录: |