Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autoHideDuration is not working in Snackbar @material-ui

I have used Snackbar from Material-ui to display an alert. I want to hide that Snackbar automatically after 5 seconds but autoHideDuration is not working .

<Snackbar
  autoHideDuration={3000}
  open={true}
  ContentProps={{
    'aria-describedby': 'message-id',
  }}
  message={<span id="message-id"> Message </span>}
/>

See screenshot

like image 576
Arun Kumar Avatar asked Mar 19 '19 10:03

Arun Kumar


2 Answers

You also have to implement the onClose method of the Snackbar component in order to make the timeout work.

Let's say the open status of the Snackbar is in your component state:

<Snackbar
  autoHideDuration={3000}
  open={this.state.open}
  ContentProps={{
    'aria-describedby': 'message-id',
  }}
  message={<span id="message-id"> Message </span>}
  onClose={() => this.setState({open: false})}
/>
like image 153
Arnaud Christ Avatar answered Oct 16 '22 11:10

Arnaud Christ


if you use functional component you can use react hooks and make it easy.

const [open, setOpen] = useState(false);
const handleClose = (event, reason) => {
    if (reason === 'clickaway') {
      return;
    }
    setOpen(false);
};
    
<Snackbar autoHideDuration={6000} anchorOrigin={{ vertical: "top", horizontal: "right" }} open={open}  onClose={handleClose}>
   <Alert onClose={handleClose} severity="success>
      "Data Successfully Submitted"
   </Alert>
</Snackbar>
like image 33
Abhushit Chaudhary Avatar answered Oct 16 '22 13:10

Abhushit Chaudhary