Skip to content

Instantly share code, notes, and snippets.

@jaouadballat
Created June 3, 2019 15:50
Show Gist options
  • Save jaouadballat/1ea2a66a59b7387161a5db46eb4a45a9 to your computer and use it in GitHub Desktop.
Save jaouadballat/1ea2a66a59b7387161a5db46eb4a45a9 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from 'react';
import Bus from '../Utils/Bus';
import './index.css';
export const Flash = () => {
let [visibility, setVisibility] = useState(false);
let [message, setMessage] = useState('');
let [type, setType] = useState('');
useEffect(() => {
Bus.addListener('flash', ({message, type}) => {
setVisibility(true);
setMessage(message);
setType(type);
setTimeout(() => {
setVisibility(false);
}, 4000);
});
}, []);
useEffect(() => {
if(document.querySelector('.close') !== null) {
document.
querySelector('.close').
addEventListener('click', () => setVisibility(false));
}
})
return (
visibility && <div className={`alert alert-${type}`}>
<span className="close"><strong>X</strong></span>
<p>{message}</p>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment