简体   繁体   English

是否可以在反应组件中使用条件?

[英]Is it possible to use conditional inside a react component?

Im using Material UI and CardHeader is a component (the top part of a post).我使用 Material UI 和 CardHeader 是一个组件(帖子的顶部)。 I want to render 2 button on the post if the post ( isUser = true )is posted by the user.如果帖子( isUser = true )由用户发布,我想在帖子上呈现 2 按钮。 Is that possible?那可能吗?

            <CardHeader
                avatar={
                    <Avatar sx={{ bgcolor: "red" }} aria-label="recipe">
                        U
                    </Avatar>
                }

                title={username}
                
                {isUser && (
                    <Box display="flex">
                        <IconButton onClick={handleEdit} sx={{ marginLeft: "auto" }}>
                            <EditIcon />
                        </IconButton>
                        <IconButton onClick={handleDelete}>
                            <DeleteForeverIcon />
                        </IconButton>
                    </Box>
                )}
            />

Assuming you want those buttons to appear in the card header's action area, you just need to assign them to the action prop...假设您希望这些按钮出现在卡片标题的操作区域中,您只需将它们分配给action道具...

<CardHeader
  avatar={
    <Avatar sx={{ bgcolor: "red" }} aria-label="recipe">
      U
    </Avatar>
  }
  title={username}
  action={
    isUser && (
      <Box display="flex">
        <IconButton onClick={handleEdit} sx={{ marginLeft: "auto" }}>
          <EditIcon />
        </IconButton>
        <IconButton onClick={handleDelete}>
          <DeleteForeverIcon />
        </IconButton>
      </Box>
    )
  }
/>;

For more details on the component's available props, see the documentation .有关组件可用道具的更多详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应组件内部函数的条件渲染不起作用 - React Conditional Rendering of Component Inside Function Not Working 您将如何在 React.Component class 中使用条件挂钩 - How would you use conditional hooks inside a React.Component class 反应:在另一个组件内部使用组件 - React: Use component inside another component 如何在 React 中使用地图内的条件渲染 - How to use conditional rendering inside map in React 如何在 React JS 的功能组件中条件渲染 map - How to conditional render inside map in a functional component of react js React:在componentDidMount内部的条件setTimeout中更新状态不会重新加载组件 - React: Updating state in a conditional setTimeout inside componentDidMount does not reload the component 关于如何在React组件的条件语句中返回的方法,是否有解决方法? - Is there a workaround as to how to return inside a conditional statement in a React component? 是否可以在反应中强制一个组件在另一个组件内部使用? - Is possible to force a component to be used inside another component in react? 是否可以将 Graphql 查询与 React Class 组件一起使用? - Is it possible to use Graphql query with React Class component? 可以在反应组件之外使用handleChange function 吗? - Possible to use handleChange function outside of react component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM