mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
27 lines
637 B
TypeScript
27 lines
637 B
TypeScript
import React, { createContext, useState, useEffect, useRef } from 'react';
|
|
|
|
export const SnackbarContext = createContext(null);
|
|
|
|
function SnackbarProvider({ children }) {
|
|
const [alert, setAlert] = useState(null);
|
|
const timer = useRef(null);
|
|
|
|
useEffect(() => {
|
|
if (alert) {
|
|
clearTimeout(timer.current);
|
|
timer.current = setTimeout(() => {
|
|
setAlert(null);
|
|
}, 2000);
|
|
}
|
|
return () => clearTimeout(timer.current);
|
|
}, [alert]);
|
|
|
|
return (
|
|
<SnackbarContext.Provider value={{ alert, addAlert: setAlert }}>
|
|
{children}
|
|
</SnackbarContext.Provider>
|
|
);
|
|
}
|
|
|
|
export default SnackbarProvider;
|