最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set timeIntervaltimeOut for alert in reactJS? - Stack Overflow

programmeradmin4浏览0评论

I have an alert for onclick event. But I want alert to last for two seconds and close automatically after 2 second. How can I set time out for alert in 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">&times;</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>

I have an alert for onclick event. But I want alert to last for two seconds and close automatically after 2 second. How can I set time out for alert in 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">&times;</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>
Share Improve this question asked Apr 28, 2019 at 9:46 MPKMPK 2473 silver badges23 bronze badges 1
  • Hi @madhav koirala, I've just provided an answer for you. Let me know if you have any questions. – Cat_Enthusiast Commented Apr 28, 2019 at 10:02
Add a ment  | 

1 Answer 1

Reset to default 5

Something like this? :

const handleShow1 = () => {
  this.setState({
     show1: true
  })

  setTimeout(() => {
     this.setState({
         show1: false
     })
  }, 2000)

}

Also here's a sandbox I made for you to get a good idea of it: https://codesandbox.io/s/2w03nvvjnp

发布评论

评论列表(0)

  1. 暂无评论