<Buttonvariant="outlined"onClick={handleClick}>
Open success snackbar
</Button><Snackbaropen={open}autoHideDuration={6000}onClose={handleClose}><AlertonClose={handleClose}severity="success"sx={{ width:'100%'}}>
This is a success message!</Alert></Snackbar><Alertseverity="error">This is an error message!</Alert><Alertseverity="warning">This is a warning message!</Alert><Alertseverity="info">This is an information message!</Alert><Alertseverity="success">This is a success message!</Alert>
<ButtononClick={handleClick(TransitionLeft)}>Right</Button><ButtononClick={handleClick(TransitionUp)}>Up</Button><ButtononClick={handleClick(TransitionRight)}>Left</Button><ButtononClick={handleClick(TransitionDown)}>Down</Button><Snackbaropen={open}onClose={handleClose}TransitionComponent={transition}message="I love snacks"key={transition ? transition.name :''}/>
Press Enter to start editing
补充项目
For more advanced use cases you might be able to take advantage of:
notistack
以下例子演示了如何使用 notistack。 notistack 有一个 imperative API 可以轻松地显示一串消息条,且无需处理其打开/关闭状态。 It also enables you to stack them on top of one another (although this is discouraged by the Material Design guidelines).
当消息条打开时,如果Escape 键被按下,每个Snackbar将会消失。 Unless you don't handle onClose with the "escapeKeyDown" reason. Unless you don't handle onClose with the "escapeKeyDown" reason. If you want to limit this behavior to only dismiss the oldest currently open Snackbar call event.preventDefault in onClose.
exportdefaultfunctionMyComponent(){const[open, setOpen]= React.useState(true);return(<React.Fragment><Snackbaropen={open}onClose={(event, reason)=>{// `reason === 'escapeKeyDown'` if `Escape` was pressedsetOpen(false);// call `event.preventDefault` to only close one Snackbar at a time.}}/><Snackbaropen={open}onClose={()=>setOpen(false)}/></React.Fragment>);}}}/><Snackbaropen={open}onClose={()=>setOpen(false)}/></React.Fragment>);}