我正试图根据本节文档重新创建Material UI Customized Dialog组件,我不明白为什么回调函数onClose
条件用于在DialogTitle
:中显示IconButton
组件
<DialogTitle sx={{ m: 0, p: 2 }} {...other}>
{children}
{onClose ? (
<IconButton
aria-label="close"
onClick={onClose}
sx={{
position: 'absolute',
right: 8,
top: 8,
color: (theme) => theme.palette.grey[500],
}}
>
<CloseIcon />
</IconButton>
) : null}
</DialogTitle>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
据我所知,如果您正确设置了状态,IconButton
总是被渲染的。我只是想知道在三元运算符中使用onClick
回调函数是否有特定的原因或模式?我试图完全去掉三元运算符,一切都一样。
如果没有onClose函数,按钮将失去所有用途,我的意思是它的全部目的是运行回调。因此,如果没有回调,那么就不要显示按钮。