gpt4 book ai didi

reactjs - Toast 未渲染(react-toastify 组件)

转载 作者:行者123 更新时间:2023-12-03 13:17:04 25 4
gpt4 key购买 nike

我正在使用 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 可能有问题,但是什么问题呢?我只是使用文档中的示例代码。

感谢您的帮助!

最佳答案

您还必须导入'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>
);
}
}

关于reactjs - Toast 未渲染(react-toastify 组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49378743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com