18910140161

JavaScript-使用React和Material-UI的徽章实现-堆栈溢出

顺晟科技

2022-10-18 13:46:47

80

我正在尝试在组件附加注释时显示徽章图标。我已经尝试了三种方法,我得到了相同的结果,我觉得很奇怪。哪一个是最好的方法来实现这一点?或者还有别的办法吗?

第一从'@material-UI/core/badge '导入徽章;

function NotesBadge({ hasNotes }) {
    
    return (
        <Badge
            anchorOrigin={{
                vertical: 'top',
                horizontal: 'right',}}
            variant="dot"
            color="primary"
            invisible={hasNotes}>
        </Badge>
    );
}

export default NotesBadge;
第二

import Badge from '@material-ui/core/Badge';

function NotesBadge({ hasNotes }) {
    
    return (
        <Badge
            anchorOrigin={{
                vertical: 'top',
                horizontal: 'right',}}
            variant="dot"
            color="primary"
            invisible={!hasNotes}>
        </Badge>
    );
}

export default NotesBadge;

第三

import Badge from '@material-ui/core/Badge';

function NotesBadge({ hasNotes }) {
    if(hasNotes){
    return (
        <Badge
            anchorOrigin={{
                vertical: 'top',
                horizontal: 'right',}}
            variant="dot"
            color="primary"
            invisible={false}>
        </Badge>
    );
}
}
export default NotesBadge;


顺晟科技:

第二种方法是可行的,因为当hasNotes为false时,hasNotes必须是不可见的,因此得名。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航