javascript - 使用 react-toastify 时出错,toast 不查看
问题描述
我明白了
Uncaught TypeError: Object(...) is not a function at useToastContainer (react-toastify.esm.js?146c:296) at ToastContainer (react-toastify.esm.js?146c:954) at mountIndeterminateComponent (react-dom. development.js?61bb:14563) 在 beginWork (react-dom.development.js?61bb:15063) 在 performUnitOfWork (react-dom.development.js?61bb:17820) 在 workLoop (react-dom.development.js?61bb :17860) 在 HTMLUnknownElement.callCallback (react-dom.development.js?61bb:149) 在 Object.invokeGuardedCallbackDev (react-dom.development.js?61bb:199) 在 invokeGuardedCallback (react-dom.development.js?61bb: 256) 在 replayUnitOfWork (react-dom.development.js?61bb:17107)
我的课
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
export class ViewAllGraphs extends React.Component {
constructor(props) {
super(props);
this.notify = this.notify.bind(this);
}
notify() {
console.log("call notify");
toast("Wow so easy !");
}
addGraph(id) {
console.log("addGraph");
console.log(id);
console.log(toast);
toast.configure();
this.notify();
}
并渲染方法返回
<img onClick={this.addGraph.bind(this, item.id)} src={`data:image/png;base64,${item.image}`} />
我是 js 新手,如何通过单击图像来调用 toast?
我的控制台日志
addGraph ViewAllGraphs.js?5897:52 6011412ff65b72973fc0f495
ViewAllGraphs.js?5897:53//它的 id ƒ toast(content, options) {
return dispatchToast(content, mergeOptions(TYPE.DEFAULT, options)); }
ViewAllGraphs.js?5897:54 调用通知 ViewAllGraphs.js?5897:41
解决方案
您需要记住<ToastContainer />
在渲染中包含 。这是一个为您提供的工作代码以及一个沙盒:
import React from "react";
import "./styles.css";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
export default class extends React.Component {
constructor(props) {
super(props);
this.notify = this.notify.bind(this);
}
notify() {
console.log("call notify");
toast.success("This is a test success", {
position: toast.POSITION.TOP_CENTER,
autoClose: 2000,
hideProgressBar: true
});
}
render() {
return (
<div>
Click the image
<br />
<img onClick={this.notify} src="https://via.placeholder.com/140x100" />
<ToastContainer />
</div>
);
}
}
推荐阅读
- arrays - 从包含 $string 的数组中回显 url
- ios - 如何在 iOS 中使用 NSKeyedArchiver 将已写入文件系统的 Flutter 中的文件作为字符串读取?
- flutter - 如何使用 MockBloc 实现小部件测试?
- google-apps-script - 谷歌应用脚本获取谷歌幻灯片中一行的宽度和高度
- spring-boot - Spring-boot TransactionRequiredException:执行更新/删除查询
- android - Firebase 消息传递 FCM 在可配置的时间间隔内分发
- python-3.x - 如何从python在excel文件上添加一个空行
- javascript - 防止在路由查询参数更改时重新获取 api
- python - 点对点迭代后如何计算结果的总和,不包括重复
- ms-access - 更改每个访问对象中的表字段名称