您好,我是新手,我正在尝试使用小吃栏组件https://material-ui.com/es/components/snackbars/#customized-snackbars,但是我不确定是否必须为演示中显示的每种警报创建单独的组件吗?
答案 0 :(得分:0)
这里是示例:
import React, {useState} from "react";
import Button from "@material-ui/core/Button";
import Snackbar from "@material-ui/core/Snackbar";
export default function SnackbarExample() {
const [state, setState] = useState({
open: false,
vertical: 'top',
horizontal: 'center',
});
const {vertical, horizontal, open} = state;
const handleClick = (newState) => () => {
setState({open: true, ...newState});
};
const handleClose = () => {
setState({...state, open: false});
};
return (
<div>
<Button onClick={handleClick({vertical: 'top', horizontal: 'center'})}>Top-Center</Button>
<Button onClick={handleClick({vertical: 'top', horizontal: 'right'})}>Top-Right</Button>
<Button onClick={handleClick({vertical: 'bottom', horizontal: 'right'})}>
Bottom-Right
</Button>
<Button onClick={handleClick({vertical: 'bottom', horizontal: 'center'})}>
Bottom-Center
</Button>
<Button onClick={handleClick({vertical: 'bottom', horizontal: 'left'})}>Bottom-Left</Button>
<Button onClick={handleClick({vertical: 'top', horizontal: 'left'})}>Top-Left</Button>
<Snackbar
anchorOrigin={{vertical, horizontal}}
key={`${vertical},${horizontal}`}
open={open}
onClose={handleClose}
message="I love snacks"
/>
</div>
);
}