Created
June 3, 2019 15:50
-
-
Save jaouadballat/1ea2a66a59b7387161a5db46eb4a45a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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