【发布时间】:2021-12-23 08:44:07
【问题描述】:
使用自定义挂钩显示和隐藏 toast,当在外部使用此挂钩时,在 Toast 组件按钮中调用 showTost 时,它可以正常工作,即在 App 组件按钮中出现错误
TypeError
Cannot read properties of null (reading 'defaultPrevented')
Toast.show
https://izso9.csb.app/node_modules/bootstrap/dist/js/bootstrap.esm.js:3399:19
showToast
/src/useToast.js:11:12
8 |
9 | const showToast = () => {
10 | bsToast = new Toast(myToast, { autohide: true, delay: 1000 });
> 11 | bsToast.show();
| ^
12 | };
13 |
14 | const hideToast = () => {
View compiled
使用Toast.js
import { Toast } from "bootstrap";
import { useRef } from "react";
export default function useToast() {
const toastRef = useRef();
let myToast = toastRef.current;
let bsToast = Toast.getInstance(myToast);
const showToast = () => {
bsToast = new Toast(myToast, { autohide: true, delay: 1000 });
bsToast.show();
};
const hideToast = () => {
bsToast = new Toast(myToast, { autohide: true });
bsToast.hide();
};
return { toastRef, showToast, hideToast };
}
【问题讨论】:
-
单击任一按钮时,我看到同样的错误。
标签: javascript reactjs react-hooks bootstrap-5