我的应用中有一个模组页面,只有版主和管理员才能访问。如果有人试图通过直接输入 URL 来访问它,我有一些逻辑可以重定向他们。
import { useHistory } from "react-router";
import createNotification from "../../../addNotification";
import getRole from "../../../authentication";
const ModHome = () => {
const history = useHistory();
const role = getRole();
if (!["administrator", "moderator"].includes(role)) {
createNotification("You are not a moderator", "error");
history.push("/admin-login");
}
return (
<div>
<h1>Mod page</h1>
</div>
)
}
export default ModHome
逻辑工作正常。我被重定向到登录页面,但由于某种原因发生了这种情况
我已经检查过了,吐司有不同的 ID,并且该函数只被调用一次,为什么我会收到 2 个通知?
更新:这是 createNofication 代码:
import {toast} from "react-toastify";
export default function createNotification(message, type) {
const options = {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
progress: undefined,
};
switch(type) {
case "success":
toast.success(message, options);
break;
case "warning":
toast.warn(message, options);
break;
case "error":
toast.error(message, options);
break;
case "info":
toast.info(message, options);
break;
}
}