mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 06:28:51 +00:00
33 lines
715 B
TypeScript
33 lines
715 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)
|
|
}, 1000);
|
|
}
|
|
return () => clearTimeout(timer.current)
|
|
}, [alert]);
|
|
|
|
|
|
const addAlert = (value) => setAlert(value);
|
|
|
|
const value = { addAlert, alert }
|
|
|
|
return (
|
|
<SnackbarContext.Provider value={value}>
|
|
<>
|
|
{children}
|
|
</>
|
|
</SnackbarContext.Provider>
|
|
)
|
|
}
|
|
|
|
export default SnackbarProvider; |