Skip to content

Instantly share code, notes, and snippets.

@tobob
Created December 18, 2019 10:52
Show Gist options
  • Save tobob/b3d122d185f091a6f1dc84f5609fec3c to your computer and use it in GitHub Desktop.
Save tobob/b3d122d185f091a6f1dc84f5609fec3c to your computer and use it in GitHub Desktop.
import React, {
createContext,
useState,
useEffect,
useRef,
useCallback,
} from 'react';
const initialToast = {
message: '',
type: null,
visible: false,
};
export const ToastContext = createContext({});
export const ToastProvider = ({children}) => {
const [toast, setToast] = useState(initialToast);
const timeout = useRef();
const show = useCallback(args => {
setToast({...initialToast, visible: true, ...args});
}, []);
const hide = useCallback(() => {
setToast({...toast, visible: false});
}, [toast]);
return (
<ToastContext.Provider
value={{
hide,
show,
toast,
}}>
{children}
</ToastContext.Provider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment