我有onclick事件的警报。但是我希望警报持续两秒钟,并在2秒后自动关闭。如何在reactJS中设置警报超时时间?
const handleHide1 = () => this.setState({ show1: false });
const handleShow1 = () => this.setState({ show1: true });
return(
<div>
<Alert show={this.state.show1} variant="success" style={{marginTop:'2%'}} onHide={this.handleHide1} >
<Alert.Heading closeButton>Order Accepted
<button type="button" class="close" onClick={handleHide1} aria-label="Close">
<span aria-hidden="true">×</span></button>
</Alert.Heading>
<p>Thank you for Accepting Order. We will inform the customer</p>
</Alert>
<Button variant="outline-success" onClick={()=>{handleShow1}>Accept</Button>
答案 0 :(得分:1)
像这样? :
const handleShow1 = () => {
this.setState({
show1: true
})
setTimeout(() => {
this.setState({
show1: false
})
}, 2000)
}
这也是我为您准备的一个沙盒,让您对它有所了解: https://codesandbox.io/s/2w03nvvjnp