Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/components/ui/Toast.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import React, { useEffect } from "react";
import React, { useEffect, useRef } from "react";
import { motion } from "framer-motion";

export const Toast = ({ message, type = "success", onClose }) => {
// Keep the latest onClose in a ref so the auto-dismiss timer always calls
// the most recent callback without onClose needing to be a reactive
// dependency. This prevents the 3-second timer from resetting every time
// the parent re-renders and passes a new inline onClose function.
const onCloseRef = useRef(onClose);

useEffect(() => {
const timer = setTimeout(onClose, 3000);
return () => clearTimeout(timer);
onCloseRef.current = onClose;
}, [onClose]);

useEffect(() => {
const timer = setTimeout(() => {
onCloseRef.current();
}, 3000);
return () => clearTimeout(timer);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<motion.div
initial={{ opacity: 0, y: 10 }}
Expand All @@ -27,4 +40,4 @@ export const Toast = ({ message, type = "success", onClose }) => {
);
};

export default Toast;
export default Toast;
Loading